How to use jQuery to select a dropdown option?

dotty picture dotty · Feb 1, 2011 · Viewed 374.6k times · Source

I was wondering if it’s possible to get jQuery to select an <option>, say the 4th item, in a dropdown box?

<select>
    <option></option>
    <option></option>
    <option></option>
    <option></option>
    <option></option>
</select>

I want the user to click a link, then have the <select> box change its value, as if the user has selected it by clicking on the <option>.

Answer

Gabriele Petrioli picture Gabriele Petrioli · Feb 1, 2011

How about

$('select>option:eq(3)').attr('selected', true);

example at http://www.jsfiddle.net/gaby/CWvwn/


for modern versions of jquery you should use the .prop() instead of .attr()

$('select>option:eq(3)').prop('selected', true);

example at http://jsfiddle.net/gaby/CWvwn/1763/