How to select the first element in the dropdown using jquery?
Solution 1:
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
Solution 2:
Your selector is wrong, you were probably looking for
$('select option:nth-child(1)')
This will work also:
$('select option:first-child')
Solution 3:
Ana alternative Solution for RSolgberg, which fires the 'onchange'
event if present:
$("#target").val($("#target option:first").val());
How to make first option of <select > selected with jQuery?
Solution 4:
$("#DDLID").val( $("#DDLID option:first-child").val() );
For Complete Drop Down List Operations using Jquery
Solution 5:
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]")