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

How do you get the current <option> of a <select> element using JavaScript?

+41
javascript html html-select
Jul 21 '10 at 16:42
source share
3 answers

This will do it for you:

 var yourSelect = document.getElementById( "your-select-id" ); alert( yourSelect.options[ yourSelect.selectedIndex ].value ) 
+77
Jul 21 '10 at 16:46
source share

.selectedIndex of the select object has an index; you can use this to index into an .options array.

+16
Jul 21 '10 at 16:45
source share
 var payeeCountry = document.getElementById( "payeeCountry" ); alert( payeeCountry.options[ yourSelect.selectedIndex ].value ); 
+2
Nov 21 '16 at 14:35
source share



All Articles