How to use jQuery to select a dropdown option?
How about
$('select>option:eq(3)').attr('selected', true);
Example:
for modern versions of jquery you should use the .prop()
instead of .attr()
$('select>option:eq(3)').prop('selected', true);
Example:
The solution:
$("#element-id").val('the value of the option');
HTML select elements have a selectedIndex
property that can be written to in order to select a particular option:
$('select').prop('selectedIndex', 3); // select 4th option
Using plain JavaScript this can be achieved by:
// use first select elementvar el = document.getElementsByTagName('select')[0]; // assuming el is not null, select 4th optionel.selectedIndex = 3;