Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending options to select using jQuery doesn't work

So, I am trying to append the options to the HTML5 select using jquery but it doesnt work for some reason. Following is my code:

    for (var i = 0; i < itemsList.length; i++) {
        WL.Logger.debug(itemsList[i]);
        var elem = $("<option/>").val(itmesList[i]).text(itemsList[i]);     
        $('#itemsList').append(elem);
    }

Here itemsList is an array of items whose value i want to append to the select dropdown box whose id is itemsList as well. here is the html code:

<div id="wrapper">
<label for="itmesList">Select item: </label>
<select id="itmesList"></select>
<div id="info"></div>
</div>

Any ideas what I am doing wrong here? Thanks!

like image 432
Cute_Ninja Avatar asked Oct 17 '12 03:10

Cute_Ninja


2 Answers

Yes.

<select id="itmesList">
$('#itemsList')
itmesList != itemsList

Edit

Also here.

itmesList[i]

When you see it.

like image 145
Ohgodwhy Avatar answered Sep 27 '22 23:09

Ohgodwhy


HTML:

<div id="wrapper">
<label for="itmesList">Select item: </label>
<select id="selectItem"></select>
<div id="info"></div>
</div>

JQuery:

var itemsList = ['a', 'b', 'c'];
var options = "";
for (var i = 0; i < itemsList.length; i++) {
    alert(itemsList.length);

    options += '<option value= "' + itemsList[i] + '">' + itemsList[i] + '</option>';

}
$('#selectItem').html(options);

DEMO: http://jsfiddle.net/Simplybj/WKfak/

like image 30
mesimplybj Avatar answered Sep 27 '22 22:09

mesimplybj