I want to select an option on a html select. For example, a select with:
<select id="list">
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
</select>
I can do something like this if I know the value for the option, for example:
<option value="0">Jan</option>
$("#list option:eq(0)").attr("selected", "selected");
Can I use the value between the option tag?
Use .val(value), Here in example I have use Mar to set as value
Set the value of each element in the set of matched elements.
$("#list").val('Mar')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="list">
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
</select>
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