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

Spread the love

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

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

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

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

For instance, we write

document.getElementById("test").options[
  document.getElementById("test").selectedIndex
].text;

to select the select element with getElementById.

We get the options with the options property.

And we get the index of the selected option with document.getElementById("test").selectedIndex.

We use it to get the option element selected from options.

Finally we use the text property to get the text of the selected option.

Conclusion

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

Leave a Reply

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