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

  • Home
  • SEARCH
  • 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 1049101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:33:44+00:00 2026-05-16T16:33:44+00:00

I have been using the following code (with jQuery v1.4.2) to set the ‘selected’

  • 0

I have been using the following code (with jQuery v1.4.2) to set the ‘selected’ attribute of a select list based on its ‘text’ description rather than its ‘value’:

$("#my-Select option[text=" + myText +"]").attr("selected","selected") ;

This code worked fine, until I noticed one select list on which it failed, depending on the text that was being matched. After some hair-pulling I realized that it was failing only in cases where the text was a single word (no spaces, no non-alpha characters). (All of my previous uses of this code had worked fine with select lists comprised solely of multi-word chemical names.)

For example, within the one select list, it worked fine with:
pharbitic acid
25-D-spirosta-3,5-diene
pogostol (#Pogostemon#)

It failed with:
glucose
adenine

I have tried any way I could think of to surround the text variable with quotes (both single and double) to no avail. (But why should a single word need quotes when a two word phrase does not?)

I have tried hard coding the text in there and had the same result.

This works:

$("#constituent option[text=#a#-allocryptopine]").attr('selected', 'selected');
  

This works:

$("#constituent option[text=5-O-methylrisanrinol]").attr('selected', 'selected');

This did not work:

$("#constituent option[text=adenine]").attr('selected', 'selected');

I tried hard coding quotes. This did not work:

$("#constituent option[text='glucose']").attr('selected', 'selected');

I could not get hard coded quotes (single or double) to work with any text at all.

It’s worth noting that quotes are acceptable when using the ‘value’ attribute. E.g., both of these work fine:

$("#constituent option[value='3']").attr('selected', 'selected');

$("#constituent option[value=3]").attr('selected', 'selected');

Below is some code to demonstrate the problem. Two select lists, the first of which is comprised of simple words, the second of two word phrases. When the page loads it tries to set the value of each select list. The jQuery code works for the second list but not the first. (I tried putting a space in ‘monkey’ to get ‘mon key’ and it worked!)

A working demo of the code below is here.

I would greatly appreciate any insight into what I am doing wrong here. Or even an alternative selector syntax for using the ‘text’ attribute.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head>
    
  <script type="text/javascript" src="../js/jquery/jquery.js"></script>
    
  <script type="text/javascript">
    
  $(document).ready(function(){

    var text1 = 'Monkey';
    $("#mySelect1 option[text=" + text1 + "]").attr('selected', 'selected');
        
    var text2 = 'Mushroom pie';
    $("#mySelect2 option[text=" + text2 + "]").attr('selected', 'selected');        
    
  });
    
  </script> 

</head>

<body>

  <select id="mySelect1">
    <option></option>
    <option>Banana</option>
    <option>Monkey</option>
    <option>Fritter</option>
  </select> 

  <select id="mySelect2">
    <option></option>
    <option>Cream cheese</option>
    <option>Mushroom pie</option>
    <option>French toast</option>
  </select> 

</body>

</html>
  • 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-16T16:33:45+00:00Added an answer on May 16, 2026 at 4:33 pm

    When an <option> isn’t given a value="", the text becomes its value, so you can just use .val() on the <select> to set by value, like this:

    var text1 = 'Monkey';
    $("#mySelect1").val(text1);
    
    var text2 = 'Mushroom pie';
    $("#mySelect2").val(text2);
    

    You can test it out here, if the example is not what you’re after and they actually have a value, use the <option> element’s .text property to .filter(), like this:

    var text1 = 'Monkey';
    $("#mySelect1 option").filter(function() {
        return this.text == text1; 
    }).attr('selected', true);
    
    var text2 = 'Mushroom pie';
    $("#mySelect2 option").filter(function() {
        return this.text == text2; 
    }).attr('selected', true);​
    

    You can test that version here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.