Sometimes, we want to get selected option text using React.
In this article, we’ll look at how to get selected option text using React.
How to get selected option text using React?
To get selected option text using React, we use the properties in the event object.
For instance, we write
const { options, selectedIndex } = e.target;
console.log(options[selectedIndex].innerHTML);
in the change event handler to get the options and selectedIndex properties from the e.target drop down.
options has all the option elements in the drop down.
selectedIndex has the index of the option selected.
Then we get the option selected with options[selectedIndex].
And we get its content with innerHTML.
Conclusion
To get selected option text using React, we use the properties in the event object.