Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve the zero-based index of the selected option in a select box?

Let's say I have the following in my HTML code:

  <select name="Currency" id="Currency">
    <option value="0.85">Euro</option>
    <option value="110.33">Japanese Yen</option>
    <option value="1.2">Canadian Dollars</option>
  </select>

Using jQuery, I can use $("#Currency").val() to give me the selectd value, and I can use $("#Currency :selected").text() to get the selected text.

What do I need to do to get the zero-based index (in this case, 0, 1, or 2) of the current selection?

like image 962
Ben McCormack Avatar asked Feb 02 '26 06:02

Ben McCormack


1 Answers

You can get the selectedIndex attribute:

var index = $("#Currency").attr("selectedIndex");

Check an example here.

like image 125
Christian C. Salvadó Avatar answered Feb 04 '26 19:02

Christian C. Salvadó



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!