views:

26193

answers:

7

I have a select control, and in a javascript variable I have a text string.

Using jQuery I want to set the selected element of the select control to be the item with the text description I have (as opposed to the value, which I don't have).

I know setting it by value is pretty trivial. e.g.

$("#my-select").val(myVal);

But I'm a bit stumped on doing it via the text description. I guess there must be a way of getting the value out from the text description, but my brain is too Friday afternoon-ed to be able to work it out.

Any ideas SO people?

+3  A: 

I haven't tested this, but this might work for you.

$("select#my-select option")
   .each(function() { this.selected = (this.text == myVal); });
spoulson
A: 

Get the children of the select box; loop through them; when you have found the one you want, set it as the selected option; return false to stop looping.

geowa4
+23  A: 

val() should handle both cases. Are you not seeing it?

Eg:

<select>
    <option value="0">One</option>
    <option value="1">Two</option>
</select>

$('select').val('1'); // selects "Two"
$('select').val('Two'); // also selects "Two"
Crescent Fresh
This feels like it shouldn't work (it appears it could be ambiguous) but it actually should work fine for me. Thanks.
DanSingerman
@HermanD: yes I suppose a method named "val" should not be selecting an option based on anything other than the "value" attribute.
Crescent Fresh
Note that jQuery 1.4 has now changed this behavior to select by `value` if the attribute has been specified, and only select by text if the `value` attribute is missing. So in this example `$('select').val('Two')` will select the second option in 1.3.x, but will do nothing in 1.4.x.
Crescent Fresh
So, what's the best way to do it in 1.4 now?
JR Lawhorne
+4  A: 
$("#myselect option:contains('YourTextHere')").val();

will return the value of the first option containing your text description. Tested this and works.

Russ Cam
Thanks - I think this might be the version I use, as I also need to have logic for when there is no matching text, and this seems the easiest mechanism, for being able to do that.
DanSingerman
bear in mind, it will get only the value for the first option matching the text.
Russ Cam
In addition, you could chain attr("selected","selected") onto the wrapped set instead of val() and this would work similar to CarolinaJay65's answer
Russ Cam
+9  A: 

Try this...to select the option with text myText

$("#my-Select option[text=" + myText +"]").attr("selected","selected") ;
CarolinaJay65
@spoulson's answer worked for me...I tried to do it without the .each
CarolinaJay65
I like this approach.
spoulson
A: 

take a look at the jquery selectedbox plugin

selectOptions(value[, clear]):

Select options by value, using a string as the parameter $("#myselect2").selectOptions("Value 1");, or a regular expression $("#myselect2").selectOptions(/^val/i);.

You can also clear already selected options: $("#myselect2").selectOptions("Value 2", true);

vitorsilva
A: 

Its cool I used it in one of my project

Sushant Danekar