Home  >  Article  >  Web Front-end  >  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
Susan SarandonOriginal
2024-11-03 10:44:29634browse

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>

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!

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