Get selected option text with JavaScript
Here's a quick and simple way to retrieve the visible text of the selected option in a <select> dropdown using JavaScript:
The variable selectedText stores the display text from the selected option in a dropdown with the ID mySelect.
Real-time text retrieval on selection changes
For dynamic behavior, hook up the change event to the <select> element. This will let you fetch and trigger actions every time the user switches selection:
Get selected text using value attribute
Occasionally, the value attribute might be identical to the option text. Simplify text retrieval in those cases:
Caution, this works only when display text and value are equal, which isn't always true.
Improve UX with clear labels
To differentiate dropdowns, especially in forms, use label elements:
With distinct labels, end-users enjoy a smoother experience and you can reference the dropdowns conveniently in your code.
Reusable function for multiple dropdowns
Dealing with a horde of dropdowns? Fear not! A single, reusable function will ease your pain:
Was this article helpful?