Alex Rivera | Logout

Adding options to a <select> using jQuery?

Asked 2009-04-11T14:34:04.563
916

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>');
Edit
Report

1 Answer

1048

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 
    }));
});
answered 2013-01-21T21:01:00.843

Your Answer