How do you get the currently selected <option>
of a <select>
element via JavaScript?
This question is related to
javascript
html
html-select
The .selectedIndex
of the select
object has an index; you can use that to index into the .options
array.
var payeeCountry = document.getElementById( "payeeCountry" );
alert( payeeCountry.options[ yourSelect.selectedIndex ].value );
Using the selectedOptions
property:
var yourSelect = document.getElementById("your-select-id");
alert(yourSelect.selectedOptions[0].value);
It works in all browsers except Internet Explorer.
Source: Stackoverflow.com