I am trying to populate a dropdown select with an array using jQuery.
Here is my code:
// Add the list of numbers to the drop down here
You can also do this:
var list = $('#items')[0]; // HTMLSelectElement
$.each(numbers, function(index, text) {
list.options[list.options.length] = new Option(index, text);
});
The array declaration has incorrect syntax. Try the following, instead:
var numbers = [ 1, 2, 3, 4, 5]
The loop part seems right
$.each(numbers, function(val, text) {
$('#items').append( $('<option></option>').val(val).html(text) )
}); // there was also a ) missing here
As @Reigel did seems to add a bit more performance (it is not noticeable on such small arrays)