How to make the first option of <select> selected with jQuery
Solution 1:
$("#target").val($("#target option:first").val());
Solution 2:
You can try this
$("#target").prop("selectedIndex", 0);
Solution 3:
// remove "selected" from any options that might already be selected
$('#target option[selected="selected"]').each(
function() {
$(this).removeAttr('selected');
}
);
// mark the first option as selected
$("#target option:first").attr('selected','selected');
Solution 4:
When you use
$("#target").val($("#target option:first").val());
this will not work in Chrome and Safari if the first option value is null.
I prefer
$("#target option:first").attr('selected','selected');
because it can work in all browsers.
Solution 5:
Changing the value of the select input or adjusting the selected attribute can overwrite the default selectedOptions property of the DOM element, resulting in an element that may not reset properly in a form that has had the reset event called.
Use jQuery's prop method to clear and set the option needed:
$("#target option:selected").prop("selected", false);
$("#target option:first").prop("selected", "selected");