How do I always select the first item in a HTML select box using index? I do not want to use val() to select.
You can use the :first selector:
var firstOption = $('#selectId option:first');
Then you can get the option value by firstOption.val(); or the option text by firstOption.text();
And set it as selected:
//firstOption.prop('selected', true); since jQuery 1.6 is known to be faster way
firstOption.attr('selected', true);
Edit: If the only thing you want is to set the selected option, use the selectedIndex attribute:
$('#selectId').attr('selectedIndex', 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With