Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the currently selected <option> in a <select> via JavaScript?

How do you get the currently selected <option> of a <select> element via JavaScript?

like image 680
Paul D. Waite Avatar asked Jul 21 '10 16:07

Paul D. Waite


People also ask

What do you use to get the currently selected option in a select box in JavaScript?

var yourSelect = document. getElementById("your-select-id"); alert(yourSelect. selectedOptions[0]. value);

How do I get selected value in select?

To get the value of a select or dropdown in HTML using pure JavaScript, first we get the select tag, in this case by id, and then we get the selected value through the selectedIndex property. The value "en" will be printed on the console (Ctrl + Shift + J to open the console).


1 Answers

This will do it for you:

var yourSelect = document.getElementById( "your-select-id" ); alert( yourSelect.options[ yourSelect.selectedIndex ].value ) 
like image 179
Pat Avatar answered Sep 22 '22 15:09

Pat