As I noted above, you have an array of objects, so each value
in the .each()
loop is the current object. You need to extract the data appropriate for each placement in the new option
element.
$(function () {
var selectValues = [{"Selected":false, "Text":"Apple", "Value":"1"},
{"Selected":false, "Text":"Samsung","Value":"2"},
{"Selected":false, "Text":"LG", "Value":"3"}];
var brand = $('#brand-0');
brand[0].selectedIndex = -1;
$.each(selectValues, function (i, value) {
$('<option>', {
value: value.Value,
text: value.Text
}).appendTo(brand);
if (value.Selected) {
brand[0].selectedIndex = i;
}
});
});
And by the way, you don't need all those double quotes in the selectedValues
objects.
var selectValues = [{Selected:false, Text:"Apple", Value:"1"},
{Selected:false, Text:"Samsung",Value:"2"},
{Selected:false, Text:"LG", Value:"3"}];