Sometimes, we want to get an option text and value with JavaScript.
In this article, we’ll look at how to get an option text and value with JavaScript.
How to get an option text and value with JavaScript?
To get an option text and value with JavaScript, we use the options
property.
For instance, we write
const optionSelection = element.options[element.selectedIndex].text;
to get an iterable object with the select element
‘s options with element.options
.
Then we get the index of the selected element with element.selectedIndex
.
Finally, we get the selected option element’s text with text
.
Conclusion
To get an option text and value with JavaScript, we use the options
property.