bach dang bach dang - 23 days ago 10
jQuery Question

Programmatically select html select element option

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?

Answer Source

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>