Ashima
Ashima

Reputation: 4834

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!

Upvotes: 7

Views: 4136

Answers (3)

Robin Rieger
Robin Rieger

Reputation: 1194

The following will append to the end of the select

var i = 0;
        $(inputitemlist).each(function () {    // the input data....
            var itmesListValue= inputitemlist[i].Value;

            $('#itmesList').append($("<option></option>")
                                        .attr("value", itmesListValue)
                                        .text(itmesListValue));
            i = i + 1;
        });

Upvotes: 0

mesimplybj
mesimplybj

Reputation: 639

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/

Upvotes: 3

Ohgodwhy
Ohgodwhy

Reputation: 50798

Yes.

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

Edit

Also here.

itmesList[i]

When you see it.

Upvotes: 4

Related Questions