Categories
JavaScript Answers

How to retrieve the text of the selected option in the select element with JavaScript?

Spread the love

Sometimes, we want to retrieve the text of the selected option in the select element with JavaScript.

In this article, we’ll look at how to retrieve the text of the selected option in the select element with JavaScript.

How to retrieve the text of the selected option in the select element with JavaScript?

To retrieve the text of the selected option in the select element with JavaScript, we use the selectedOptions property.

For instance, we write

const selected = document.getElementById("test").selectedOptions[0].text;

to get the select element with getElementById.

Then we get the selected options from it with selectedOptions.

We get the first selected option with [0].

And we get the text of the selected option with text.

Conclusion

To retrieve the text of the selected option in the select element with JavaScript, we use the selectedOptions 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 *