Home >Web Front-end >JS Tutorial >How to Get Selected Text from a Dropdown List Using jQuery?

How to Get Selected Text from a Dropdown List Using jQuery?

DDD
DDDOriginal
2024-12-14 14:27:10579browse

How to Get Selected Text from a Dropdown List Using jQuery?

Obtain Selected Text from Drop-Down Lists with jQuery

Need to retrieve the actual text of the selected item from a drop-down list? jQuery offers a straightforward solution:

$("#yourdropdownid option:selected").text();

Let's break it down:

  • $("#yourdropdownid"): Selects the drop-down list with the specified ID.
  • option: Targets the options (list items) within the drop-down list.
  • :selected: Filters the options to get the currently selected one.
  • .text(): Extracts the text content of the selected option.

By combining these elements, you can easily retrieve the selected text as a string. For instance, if your drop-down list has the ID "mydropdown" and the selected option has the text "Red," you can obtain it like this:

var selectedText = $("#mydropdown option:selected").text();

This approach is particularly useful when you need to display or manipulate the selected text in your code, without relying on the selected value.

The above is the detailed content of How to Get Selected Text from a Dropdown List Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn