How to get the DropDown Selected Index in JavaScript?
I have a DropDown with options Mango, ASpple, Orange.
When i select an option from DropDownList and click on button i want display the selected index in JavaScript alert box.
Hi makenzi.exc,
First reference the DropDown using document.querySelector.
Then, you can find the selected index using the selectedIndex property.
Refer below example.
HTML
<html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } </style> </head> <body> Select Fruit: <select id="ddlFruits" name="Fruit"> <option value=""></option> <option value="1">Apple</option> <option value="2">Mango</option> <option value="3">Orange</option> </select> <script type="text/javascript"> function GetSelectedIndex() { // Referencing DropDownList. var ddlFruits = document.querySelector('#ddlFruits'); // Referencing Selected Index. var selectedIndex = ddlFruits.selectedIndex; alert("Selected Index: " + selectedIndex); }; </script> <input type="button" value="Submit" onclick="GetSelectedIndex()" /> </body> </html>
Demo
Screenshot
Downloads
Download Sample
© COPYRIGHT 2025 ASPSnippets.com ALL RIGHTS RESERVED.