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
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
Your selector is wrong, you were probably looking for
$('select option:nth-child(0)')
This will work also:
$('select option:first-child')
Try this out...
$('select option:first-child').attr("selected", "selected");
Another option would be something like 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:
http://stackoverflow.com/questions/499405/change-selected-value-of-drop-down-list-with-jquery
What you want is probably:
$("select option:first-child")
What this code
attr("selected", "selected");
is doing is setting the "selected" attribute to "selected"
If you want the selected options, regardless of whether it is the first-child, the selector is:
$("select").children("[selected]")