How to select the first element in the dropdown using jquery?

Amr Elgarhy picture Amr Elgarhy · Sep 8, 2009 · Viewed 218.3k times · Source

I want to know how to select the first option in all select tags on my page using jquery.

tried this:

$('select option:nth(0)').attr("selected", "selected"); 

But didn't work

Answer

RSolberg picture RSolberg · Sep 8, 2009

Try this out...

$('select option:first-child').attr("selected", "selected");

Another option would be this, but it will only work for one drop down list at a time as coded below:

var myDDL = $('myID');
myDDL[0].selectedIndex = 0;

Take a look at this post on how to set based on value, its interesting but won't help you for this specific issue:

Change the selected value of a drop-down list with jQuery