Get selected value in dropdown list using JavaScript

The selectedIndex property is utilized to both retrieve and set the index of the selected option within a dropdown list. This index corresponds to the position of the option within the list and can be manipulated through JavaScript to manage the selected option dynamically.

  1. Get from selectedIndex property.
selectObject.selectedIndex
  1. Set the Select selectedIndex property.
selectObject.selectedIndex = number

The index starts at 0 and if no element is selected then it return -1. If the drop-down list allows multiple selections it will only return the index of the first option selected.

run this source code Browser View


Full Source | JavaScript
<!DOCTYPE html> <html> <body> <div> <select id="items"> <option value="1">One</option> <option value="2">Two</option> <option value="3" selected="selected">Three</option> <option value="4">Four</option> <option value="5">Five</option> </select> <br><br> <button onClick="GetSelectedItem('items');">Get Selected Item</button> </div> <script> function GetSelectedItem(sItem) { var i = document.getElementById(sItem); var strSel = "Value is: " + i.options[i.selectedIndex].value + " and Text is: " + i.options[i.selectedIndex].text; alert(strSel); } </script> </body> </html>


Javascript drop down selected item

jQuery

$("#elementId :selected").text(); // The text content of the selected option $("#elementId :selected").val(); // The value of the selected option

Conclusion

To obtain the selected value from a dropdown list in JavaScript, retrieve the dropdown element and access its value property. This straightforward approach enables you to retrieve and process the chosen option easily.