Categories
JavaScript Answers

How to get an option text and value with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *