Reputation: 3833
I am running this script:
function insertformat(formats){
//Deletes the content of the dropdown if there was any
document.getElementById('format').options.length = 50;
//fills the dropdown with an associative array
for (var key in formats)
var newOption = "<option value='"+key+"'>"+formats[key]+"</option>";
$("#format").append(newOption);
}
to fill the following select:
<select id="format"></select>
However only the last element of the array is stored. In the debug mode it seems that the dropdown is overwritten everytime the loop is finished, instead of adding a new element.
Upvotes: 0
Views: 36
Reputation: 14604
Currently the appned
code is not inside loop
so only last value
is added to the dropdown. Change your loop like this.
for (var key in formats)
{
var newOption = "<option value='"+key+"'>"+formats[key]+"</option>";
$("#format").append(newOption);
}
Upvotes: 3
Reputation: 5528
Use this code
function insertformat(formats){
//Deletes the content of the dropdown if there was any
document.getElementById('format').options.length = 50;
//fills the dropdown with an associative array
for (var key in formats){
var newOption = "<option value='"+key+"'>"+formats[key]+"</option>";
$("#format").append(newOption);
}
}
Upvotes: 2