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?
$('#gate').val('Gateway 2').prop('selected', true);
This would be another option:
My problem
I had the same issue . The Only difference from your code is that I was loading the select box through an ajax call and soon as the ajax call was executed I had set the default value of the select box
The Solution
I had a problem where the value was not set because of a syntax error before the call.
Check for syntax errors before the call.
This definitely should work. Here's a demo. Make sure you have placed your code into a
$(document).ready
: