Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6641333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:47:54+00:00 2026-05-25T23:47:54+00:00

How to set an option in select by text using jQuery? The following works

  • 0

How to set an option in select by text using jQuery? The following works

$("#selectID option:contains('optionText')").attr("selected", true);

Is there a simpler/shorter way?

Example

<select id="fruit">
    <option value="">--select--</option>
    <option value="1">apple</option>
    <option value="2">pineapple</option>
    <option value="3">orange</option>
<select>
<br>
<button>apple</button>
<button>orange</button>
<button>pineapple</button>

$(function() {
    $("button").click(function() {
        $("#fruit option:contains('" + $(this).text() + "')").attr("selected", true);
    })
})

Demo

EDIT

The version posted above using Contains has a bug. Since the Contains would match partial strings also (e.g. apple would match pineapple), it can select incorrect options.

// Doesn't work in FF4, IE9 using jQuery 1.4.4
$('#fruit').val('apple'); // Doesn't work in FF4, IE9 using jQuery 1.4.4

// Doesn't work either in FF4, IE9 using jQuery 1.4.4
$('#fruit option[text="apple"]').attr("selected", true);

// This works
$("#fruit").children("option[text='apple']").attr("selected", true);

EDIT Feb 14th, 2012

// The above doesn't work in jQuery 1.7.1

// Using even more verbose version [Demo][2]

var buttonText = $(this).text();

$("#fruit option:contains('" + buttonText + "')")
    .filter(function(i){
        return $(this).text() === buttonText;
    })
    .attr("selected", true)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:47:55+00:00Added an answer on May 25, 2026 at 11:47 pm
    // Work in FF4, IE9 using jQuery 1.4.4 but has a bug (see original post)
    $("#selectID option:contains('optionText')").attr("selected", true);
    
    // Doesn't work in FF4, IE9 using jQuery 1.4.4
    $('#fruit').val('apple'); // Doesn't work in FF4, IE9 using jQuery 1.4.4
    
    // Doesn't work either in FF4, IE9 using jQuery 1.4.4
    $('#fruit option[text="apple"]').attr("selected", true);
    
    // This works
    $("#fruit").children("option[text='apple']").attr("selected", true);
    

    Demo

    EDIT Feb 14th, 2012

    // The above doesn't work in jQuery 1.7.1
    
    // Using even more verbose version [Demo][2]
    
    var buttonText = $(this).text();
    
    $("#fruit option:contains('" + buttonText + "')")
        .filter(function(i){
            return $(this).text() === buttonText;
        })
        .attr("selected", true)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using the following code (with jQuery v1.4.2) to set the 'selected'
I am using a jQuery function to clone a DIV that contains a set
Using jquery, I need to set the class of a div using a select
Using jQuery, I'd like to display a different set of text based on a
How to set radio option checked onload with jQuery? Need to check if no
I am creating graphs using GD::Graph::lines . For option set, I have to write
i noted that in rails 2.3.2 there is no more option to set which
I have multiple combo and option boxes whose visibility is set true or false
Is it possible to set the selected attribute of an option tag via a
I'm using Jorn Zaefferer's Autocomplete query plugin, http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ I have options set so it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.