Adding Options To A Using JQuery?


Answer :

Personally, I prefer this syntax for appending options:

$('#mySelect').append($('<option>', {     value: 1,     text: 'My option' })); 

If you're adding options from a collection of items, you can do the following:

$.each(items, function (i, item) {     $('#mySelect').append($('<option>', {          value: item.value,         text : item.text      })); }); 

This did NOT work in IE8 (yet did in FF):

$("#selectList").append(new Option("option text", "value")); 

This DID work:

var o = new Option("option text", "value"); /// jquerify the DOM object 'o' so we can use the html method $(o).html("option text"); $("#selectList").append(o); 

You can add option using following syntax, Also you can visit to way handle option in jQuery for more details.

  1. $('#select').append($('<option>', {value:1, text:'One'}));

  2. $('#select').append('<option value="1">One</option>');

  3. var option = new Option(text, value); $('#select').append($(option));


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable