javascript - jQuery - setting the selected value of a select control via its text description -


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

Using jQuery I want to set the selected element of selection control to be an item with text description (as opposed to the value, which I do not have).

I know that this value is determined by pretty trivial eg.

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

But I am slightly stumped on doing this through text descriptions. I think text should be a way to get value from the description, but my brain is also to be able to work it out even on Friday afternoon.

Looking at this HTML:

   

jQuery v1.6 +:

  var text1 = Choose from the details for 'two'; $ ("Select option"). Return to $ (this) .text () == text1;} Here you can use the filter (function () {// $$$}. Prop ('selected', true);  

Choose from the description for jQuery versions below 1.6 and greater than or equal to 1.4

  var text1 = ' two' ; $ ("Select option"). Filter (function () wants to use {$ $ $ $}, return here $ (this) .text () == text1;}). Ether ('selected' is true);  

Note that when this approach will work in versions above 1.6, but less than 1.9, it has been deprecated since 1.6. This is the in jQuery 1.9+.

Choose from the description for the previous versions:

val () should handle both the cases Do not you see it Are you

Example:

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

Comments