In the following:
<select id="test">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
How can I get the text of the selected option (i.e. "Test One" or "Test Two") using JavaScript
document.getElementsById('test').selectedValue
returns 1 or 2, what property returns the text of the selected option?
The
options
property contains all the<options>
- from there you can look at.text
Use the select list object, to identify its own selected options index. From there - grab the inner HTML of that index. And now you have the text string of that option.
Easy, simple way:
References:
this.options[this.selectedIndex].innerText
Under HTML5 you are be able to do this:
MDN's documentation at https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedOptions indicates full cross-browser support (as of at least December 2017), including Chrome, Firefox, Edge and mobile browsers, but excluding Internet Explorer.