1

I would like to replace whole content in my selectbox.

Here's what I have:

My new content looks like:

[Object, Object, Object]
0: Object
Label: "A"
Value: 1
1: Object
Label: "B"
Value: 2

and I tried replace it this way:

var $el = $("#mySelectBoxId");
$el.empty();
$.each(newOptions, function (value, key) {
$el.append($("<option></option>")
.attr("value", value).text(key));
});

And the result looks like list of Objects.

mskuratowski
  • 4,014
  • 15
  • 58
  • 109
  • 1
    `$el.append($("").val(key.Value).text(key.Label);` (the 1st parameter of the `.each` function is the index and the 2nd is the item) –  May 25 '16 at 09:47

1 Answers1

1

Try like this,

Demo JSFiddle

var $el = $("#mySelectBoxId");
$el.empty();
$.each(newOptions, function(idx,item) {
  $el.append($("<option/>").attr("value", ""+item.Value+"").text(item.Label));
});
Raja Jaganathan
  • 33,099
  • 4
  • 30
  • 33