Use jQuery to set select box value to first option
I am dynamically populating a select box with options. When I do this, I
want the value of the select box to be the value of the first option (a
'default option', if you like). Sounds really simple, but I just can't get
it to work.
var myElement = $('select[name="myName"]');
.... tried the following three variations
// myElement.find('option').first().prop('selected', 'selected');
// myElement.val(myElement.find('options').first().val());
myElement.prop('selectedIndex', 0);
...but the following line gives a blank alert
alert(myElement.val());
Where am I going wrong?
No comments:
Post a Comment