How to get the zero index of the selected parameter in the selection field?

Let's say in my HTML there is the following:

  <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 a selectd value, and I can use $("#Currency :selected").text()to get the selected text.

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

+3
source share
4 answers

You can get the attribute selectedIndex:

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

See an example here .

+6
source

$("Currency").attr("selectedIndex")

+1
source
document.getElementById("Currency").selectedIndex;
0

- javascript.

$("#Currency")[0].selectedIndex
0

Source: https://habr.com/ru/post/1740362/


All Articles