Home > Web Front-end > JS Tutorial > body text

How to Retrieve the Text of an Option from a jQuery Dropdown List?

Barbara Streisand
Release: 2024-10-27 05:22:29
Original
820 people have browsed it

How to Retrieve the Text of an Option from a jQuery Dropdown List?

Retrieving Option Text from jQuery Drop-Down List

To obtain the text associated with a specific option tag in a jQuery drop-down list, use the following technique:

Option Based on Value

To get the text of an option with a specific value, even if it is not selected, employ the selector:

$("#list option[value='2']").text();
Copy after login

This selector matches the option tag that has the value '2' and retrieves its text content.

Selected Option

To get the text of the currently selected option, regardless of its value, utilize this selector:

$("#list option:selected").text();
Copy after login

This selector matches the selected option tag and extracts its text content.

Note that the initial approach of using $("#list[value='2']").text(); will not yield the desired result, as it attempts to find the entire