title: jQuery: Setting select list 'selected' based on text, failing strangely
body: <p>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':</p>
  
  <pre><code>$("#my-Select option[text=" + myText +"]").attr("selected","selected") ;
  </code></pre>
  
  <p>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.)</p>
  
  <p>For example, within the one select list, it worked fine with:<br>
  pharbitic acid<br>
  25-D-spirosta-3,5-diene<br>
  pogostol (#Pogostemon#)   </p>
  
  <p>It failed with:<br>
  glucose<br>
  adenine</p>
  
  <p>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?)</p>
  
  <p>I have tried hard coding the text in there and had the same result.</p>
  
  <p>This works:</p>
  
  <pre><code>$("#constituent option[text=#a#-allocryptopine]").attr('selected', 'selected');
  </code></pre>
  
  <p>This works:</p>
  
  <pre><code>$("#constituent option[text=5-O-methylrisanrinol]").attr('selected', 'selected');
  </code></pre>
  
  <p>This <strong>did not work</strong>: </p>
  
  <pre><code>$("#constituent option[text=adenine]").attr('selected', 'selected');
  </code></pre>
  
  <p>I tried hard coding quotes. This <strong>did not work</strong>:</p>
  
  <pre><code>$("#constituent option[text='glucose']").attr('selected', 'selected');
  </code></pre>
  
  <p>I could not get hard coded quotes (single or double) to work with <em>any</em> text at all.</p>
  
  <p>It's worth noting that quotes are acceptable when using the 'value' attribute. E.g., both of these work fine:</p>
  
  <pre><code>$("#constituent option[value='3']").attr('selected', 'selected');
  
  $("#constituent option[value=3]").attr('selected', 'selected');
  </code></pre>
  
  <p>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!)</p>
  
  <p>A working demo of the code below is <a href="http://snubian.com/test/select.html" rel="noreferrer">here</a>.</p>
  
  <p>I would greatly appreciate any insight into what I am doing wrong here. Or even an alternative selector syntax for using the  'text' attribute. Thanks in advance to all who have the time to assist.</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; 
  
  &lt;head&gt;
  
    &lt;script type="text/javascript" src="../js/jquery/jquery.js"&gt;&lt;/script&gt;
  
    &lt;script type="text/javascript"&gt;
  
    $(document).ready(function(){
  
      var text1 = 'Monkey';
      $("#mySelect1 option[text=" + text1 + "]").attr('selected', 'selected');
  
      var text2 = 'Mushroom pie';
      $("#mySelect2 option[text=" + text2 + "]").attr('selected', 'selected');        
  
    });
  
    &lt;/script&gt; 
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
    &lt;select id="mySelect1"&gt;
      &lt;option&gt;&lt;/option&gt;
      &lt;option&gt;Banana&lt;/option&gt;
      &lt;option&gt;Monkey&lt;/option&gt;
      &lt;option&gt;Fritter&lt;/option&gt;
    &lt;/select&gt; 
  
    &lt;select id="mySelect2"&gt;
      &lt;option&gt;&lt;/option&gt;
      &lt;option&gt;Cream cheese&lt;/option&gt;
      &lt;option&gt;Mushroom pie&lt;/option&gt;
      &lt;option&gt;French toast&lt;/option&gt;
    &lt;/select&gt; 
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>