Jquery - setting the selected value of a select control via its text description?

Val() should handle both cases. Are you not seeing it? Eg: One Two $('select').

Val('1'); // selects "Two" $('select'). Val('Two'); // also selects "Two.

1 This feels like it shouldn't work (it appears it could be ambiguous) but it actually should work fine for me. Thanks. – DanSingerman Jan 30 '09 at 16:28 @HermanD: yes I suppose a method named "val" should not be selecting an option based on anything other than the "value" attribute.

– Crescent Fresh Jan 30 '09 at 16:55 16 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 Mar 3 '10 at 19:34 9 So, what's the best way to do it in 1.4 now? – JR Lawhorne Apr 3 '10 at 6:38 1 In more recent versions of jQuery, . Val('not-an-option-value') will reset the select to the first option.

– dland May 3 at 9:29.

Try this...to select the option with text myText $("#my-Select optiontext=" + myText +""). Attr("selected","selected").

Spoulson's answer worked for me...I tried to do it without the . Each – CarolinaJay65 Jan 30 '09 at 16:55 1 I like this approach. – spoulson Jan 30 '09 at 18:31 In many cases, this approach doesn't work unfortunately.

I even had to resolve to classic $("#my-Select optiontext=" + myText +""). Get(0). Selected = true; style from time to time :(... – Shehi Mar 6 at 21:15.

I haven't tested this, but this might work for you. $("select#my-select option") . Each(function() { this.

Selected = (this. Text == myVal); }).

Thanks. Works great in 1.4+ – Jagd Aug 24 at 18:21.

$("#myselect option:contains('YourTextHere')").val(); will return the value of the first option containing your text description. Tested this and works.

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 Jan 30 '09 at 17:17 1 bear in mind, it will get only the value for the first option matching the text. – Russ Cam Jan 30 '09 at 18:32 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 Jan 30 '09 at 18:57.

$("#Test"). Find("option:contains('two')"). Each(function(){ if( $(this).text() == 'two' ) { $(this).

Attr("selected","selected"); } }); The if statement does a exact match with "two" and "two three" will not be matched.

Jagd do you mean $("#Test"). Val('two') will do the trick – Web Developer Aug 27 at 14:37 No, that's not what I meant. I think my comment was a bit unclear, so I just deleted it.

I did upvote your answer though, because it worked for my situation. – Jagd Sep 20 at 18:42.

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.

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).

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.

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?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions