Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically select html select element option

Tags:

jquery

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?

like image 552
bach dang Avatar asked Mar 22 '26 20:03

bach dang


1 Answers

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>
like image 173
Satpal Avatar answered Mar 24 '26 08:03

Satpal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!