jQuery Get Selected Option From Dropdown
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In jQuery, the simplest way to get the selected option from a dropdown is usually $("#mySelect").val(). If you also need the displayed text or custom attributes from the selected option, use find("option:selected") and then read the properties from that element.
HTML Example
Start with a normal select element:
The selected option has two pieces of information developers commonly need:
- the
value - the visible text
Get the Selected Value with .val()
If you only want the selected value, .val() is the cleanest answer:
If the selected option is banana, the output is:
That is enough for many form-handling tasks.
Get the Selected Option Element
If you need more than the value, fetch the selected option element itself:
This is the pattern to use when you need the visible label or more attributes from the selected option.
Read Custom Data Attributes
If options carry extra metadata, option:selected is especially useful.
That is often cleaner than creating parallel lookup tables in JavaScript.
Get the Current Selection Immediately
You do not have to wait for a change event. If the dropdown already has a selected item and you want to read it on page load, just query it directly:
This is useful for prefilled forms or server-rendered pages.
Dynamic Dropdowns and Delegated Events
If the dropdown is added to the page later, binding directly to it at startup may miss the event. In that case, delegate from a stable ancestor:
This pattern matters in applications that render form controls dynamically through Ajax or client-side templates.
Multi-Select Dropdowns
If the select allows multiple selections, .val() returns an array of selected values instead of one string.
That distinction matters when you reuse the same handler for single-select and multi-select elements.
Common Pitfalls
- Using
.text()on theselectelement instead of the selectedoptionreturns more text than expected. - Assuming
.val()always returns a string is incorrect for multi-select dropdowns, where it can return an array. - Reading custom metadata from the
selectinstead of the selectedoptiongives the wrong element. - Forgetting that the handler runs only on
changecan make a preselected value look missing until the user interacts. - Mixing raw DOM APIs and jQuery inconsistently makes simple code harder to read than it needs to be.
Summary
- Use
$("#dropdown").val()when you only need the selected value. - Use
$("#dropdown option:selected")or.find("option:selected")when you need text or attributes. - Read custom metadata with
.data()from the selected option. - Query the dropdown directly on page load if you need the current selection before any change event.
- Be careful with multi-select elements because the return type from
.val()changes.
Related reading
- jQuery get specific option tag text
- jQuery get value of select onChange
- jQuery .geturl breaks my sequential script execution
- jQuery hasAttr checking to see if there is an attribute on an element
- jQuery how to find an element based on a data-attribute value?
- jQuery UI Sortable, then write order into a database
- jQuery's jquery-1.10.2.min.map is triggering a 404 (Not Found)
- JS How to track errors where there are many functions calls
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.