Skip to content

Instantly share code, notes, and snippets.

@davedavis
Created February 14, 2020 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davedavis/ac48bbbef51c462256fac70d3ad2d03c to your computer and use it in GitHub Desktop.
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)
/* 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