Adding options to a <select> using jQuery?

Viewed 1471578

What's the easiest way to add an option to a dropdown using jQuery?

Will this work?

$("#mySelect").append('<option value=1>My option</option>');
31 Answers

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);

If the option name or value is dynamic, you won't want to have to worry about escaping special characters in it; in this you might prefer simple DOM methods:

var s= document.getElementById('mySelect');
s.options[s.options.length]= new Option('My option', '1');

This is very simple:

$('#select_id').append('<option value="five" selected="selected">Five</option>');

or

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

That works well.

If adding more than one option element, I'd recommend performing the append once as opposed to performing an append on each element.

for whatever reason doing $("#myselect").append(new Option("text", "text")); isn't working for me in IE7+

I had to use $("#myselect").html("<option value='text'>text</option>");

Why not simply?

$('<option/>')
  .val(optionVal)
  .text('some option')
  .appendTo('#mySelect')

Based on dule's answer for appending a collection of items, a one-liner for...in will also work wonders:

let cities = {'ny':'New York','ld':'London','db':'Dubai','pk':'Beijing','tk':'Tokyo','nd':'New Delhi'};

for(let c in cities){$('#selectCity').append($('<option>',{value: c,text: cities[c]}))}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<select  id="selectCity"></select>

Both object values and indexes are assigned to the options. This solution works even in the old jQuery (v1.4)!

$('#select_id').append($('<option>',{ value: v, text: t }));

This is just a quick points for best performance

always when you are dealing with many options, build a big string and then add it to the 'select' for best performance

f.g.

var $mySelect = $('#mySelect'); var str = '';

$.each(items, function (i, item) {
    // IMPORTANT: no selectors inside the loop (for the best performance)
    str += "<option value='" + item.value + "'> " + item.text + "</option>";
});
// you built a big string

$mySelect.html(str); // <-- here you add the big string with a lot of options into the selector.
$mySelect.multiSelect('refresh');

Even faster

var str = ""; 
for(var i; i = 0; i < arr.length; i++){
    str += "<option value='" + item[i].value + "'> " + item[i].text + "</option>";
}    
$mySelect.html(str);
$mySelect.multiSelect('refresh');
$(function () {
     var option = $("<option></option>");
     option.text("Display text");
     option.val("1");
     $("#Select1").append(option);
});

If you getting data from some object, then just forward that object to function...

$(function (product) {
     var option = $("<option></option>");
     option.text(product.Name);
     option.val(product.Id);
     $("#Select1").append(option);
});

Name and Id are names of object properties...so you can call them whatever you like...And ofcourse if you have Array...you want to build custom function with for loop...and then just call that function in document ready...Cheers

You can do this in ES6:

$.each(json, (i, val) => {
    $('.js-country-of-birth').append(`<option value="${val.country_code}"> ${val.country} </option>`);
  });

If someone comes here looking for a way to add options with data properties

Using attr

var option = $('<option>', { value: 'the_value', text: 'some text' }).attr('family', model.family);

Using data - version added 1.2.3

var option = $('<option>', { value: 'the_value', text: 'some text' }).data('misc', 'misc-value);

Try

mySelect.innerHTML+= '<option value=1>My option</option>';

btn.onclick= _=> mySelect.innerHTML+= `<option selected>${+new Date}</option>`
<button id="btn">Add option</button>

<select id="mySelect"></select>

Just by single line

   $.each(result, function (k,v) {
                         **if(k == selectedMonthNumber) var selectedMonth = 'selected'; else var selectedMonth = '';**
                             $("#periodMonth").append($('<option>', {
                                 value: k,
                                 text: v,
                                 **selected: selectedMonth**
                         }));
                    })
Related