Home > Web Front-end > JS Tutorial > How to Select an Option in a jQuery Select Control by Text Description?

How to Select an Option in a jQuery Select Control by Text Description?

Susan Sarandon
Release: 2024-11-03 10:44:29
Original
678 people have browsed it

How to Select an Option in a jQuery Select Control by Text Description?

Selecting an Option by Text Description Using jQuery

When working with select controls, you may want to select an option based on its text description rather than its value. In jQuery, you can achieve this using a few techniques.

One approach for jQuery versions 1.6 and above is to filter the options by their text and then set the selected property:

<code class="js">var text1 = 'Two';
$("select option").filter(function() {
  return $(this).text() == text1;
}).prop('selected', true);</code>
Copy after login

This code selects the option with the text "Two" and sets it as the selected option.

The above is the detailed content of How to Select an Option in a jQuery Select Control by Text Description?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template