I have a form that lets the user select an option from a <select>
dropdown. When the form is opened again I'd like to show the dropdown set to the last value chosen by the user, which I've saved away. Is this possible with jQuery?
Thanks
I have a form that lets the user select an option from a <select>
dropdown. When the form is opened again I'd like to show the dropdown set to the last value chosen by the user, which I've saved away. Is this possible with jQuery?
Thanks
You have basically two or three options.
<select>
, you pre-select the option stored in the database. The jQuery part here could be sending an ajax request to your server to store the value.To actually do the selecting:
HTML:
<select id="sel">
<option value="o1">option 1</option>
<option value="o2">option 2</option>
...
</select>
JS:
$(function () { // run a function when page is loaded (similar to onLoad event)
var value = "o1"; // get the value to pre-select
$("#sel").val(value);
});
See it in action here.