Created
February 14, 2020 21:27
-
-
Save davedavis/ac48bbbef51c462256fac70d3ad2d03c to your computer and use it in GitHub Desktop.
Selector for select HTML element (for using with onClick as opposed to onChange)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* assuming we have the following HTML | |
<select id='s'> | |
<option>First</option> | |
<option selected>Second</option> | |
<option>Third</option> | |
</select> | |
*/ | |
var select = document.getElementById('s'); | |
// return the index of the selected option | |
console.log(select.selectedIndex); // 1 | |
// return the value of the selected option | |
console.log(select.options[select.selectedIndex].value) // Second |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment