Set selected option of select box
I want to set a option that was selected previously to be displayed on page load. I tried it with the following code:
$("#gate").val('Gateway 2');
with
<select id="gate">
<option value='null'>- choose -</option>
<option value='gateway_1'>Gateway 1</option>
<option value='gateway_2'>Gateway 2</option>
</select>
But this does not work. Any ideas?
This definitely should work. Here's a demo. Make sure you have placed your code into a $(document).ready
:
$(function() {
$("#gate").val('gateway_2');
});
$(document).ready(function() {
$("#gate option[value='Gateway 2']").prop('selected', true);
// you need to specify id of combo to set right combo, if more than one combo
});