To get the selected option’s value and visible text from a <select> dropdown using jQuery, call .val() on the <select> element for the value, and query $("#mySelect option:selected").text() (or .find("option:selected").text()) for the displayed label text.
1. Reading Both Value and Text Together
Reading both properties right when you need them—such as on a change event or form submit—keeps your state in sync with the user’s current choice.
// Read current value and displayed text from a dropdown
function getSelectedDropdownOption(selector) {
const $select = $(selector);
const $selectedOption = $select.find("option:selected");
return {
value: $select.val(),
text: $selectedOption.text().trim(),
hasSelection: $selectedOption.length > 0
};
}
// Listen for selection changes
$("#countrySelect").on("change", function () {
const value = $(this).val();
const text = $(this).find("option:selected").text().trim();
console.log("Selected value:", value, "| Selected text:", text);
});
console.log(getSelectedDropdownOption("#countrySelect"));
: Returns thevalueattribute of the active<option>, or falls back to the option’s text content if novalueattribute is set.$select.find("option:selected").text(): Returns the visible text inside the selected<option>element. Calling.trim()removes accidental leading or trailing whitespace/newlines from formatted markup.
2. Handling Placeholder Options and Multi-Select Dropdowns
Dropdowns often start with a disabled or empty placeholder option (like "Select a city"), or allow selecting multiple items (<select multiple>).
// Ignore empty placeholder values ("")
function getValidSelection(selector) {
const value = $(selector).val();
const text = $(selector).find("option:selected").text().trim();
if (!value) {
return null;
}
return { value, text };
}
// Multi-select dropdown: .val() returns an array of selected values
function getMultiSelectValues(selector) {
const values = $(selector).val() || [];
const texts = $(selector)
.find("option:selected")
.map((_, opt) => $(opt).text().trim())
.get();
return { values, texts };
}
console.log(getValidSelection("#citySelect"));
console.log(getMultiSelectValues("#skillsSelect"));
- Placeholder check: Checking
if (!value)prevents treating an empty placeholder option (value="") as a real selection. - Multi-select behavior: On
<select multiple>,.val()returns an array (ornullwhen nothing is selected), so.val() || []avoids null-access errors.
3. Common Traps to Avoid
- Using
.text()directly on the<select>element: Calling$("#mySelect").text()concatenates the text of all<option>elements inside the dropdown instead of only the selected one. - Forgetting
.trim()on option text: Indented<option>lines in source code can include extra spaces or line breaks in.text(). - Using
.attr("selected")instead of:selected: Theselectedattribute only reflects the initial HTML attribute, whereas:selectedreflects the live selection state in the DOM.
TL;DR: Use $("#mySelect").val() to get the selected option’s value and $("#mySelect").find("option:selected").text().trim() to get its visible label text, and wrap both inside a .on("change", ...) handler for live updates.
Are you reading a single dropdown on change/submit, guarding against a placeholder option, or working with a <select multiple> dropdown?