mskuratowski
mskuratowski

Reputation: 4124

Replace selectbox content

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.

Upvotes: 1

Views: 26

Answers (1)

Raja Jaganathan
Raja Jaganathan

Reputation: 36147

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));
});

Upvotes: 1

Related Questions