Get your own website Result Size: 625 x 565
x
 
<!DOCTYPE html>
<html>
<body>
Select a fruit and click the button:
<select id="mySelect">
  <option>Apple</option>
  <option>Orange</option>
  <option>Pineapple</option>
  <option>Banana</option>
</select>
<button type="button" onclick="myFunction()">Display index</button>
<script>
function myFunction() {
  var x = document.getElementById("mySelect").selectedIndex;
  var y = document.getElementById("mySelect").options;
  alert("Index: " + y[x].index + " is " + y[x].text);
}
</script>
</body>
<!-- Mirrored from www.w3schools.com/jsref/tryit.asp?filename=tryjsref_option_index by HTTrack Website Copier/3.x [XR&CO'2014], Mon, 05 Sep 2022 15:53:10 GMT -->
</html>