Skip to content
Advertisement

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

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.

JavaScript

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.

Advertisement

Answer

Select by description for jQuery v1.6+

JavaScript
JavaScript

jQuery versions below 1.6 and greater than or equal to 1.4

JavaScript
JavaScript

Note that while this approach will work in versions that are above 1.6 but less than 1.9, it has been deprecated since 1.6. It will not work in jQuery 1.9+.


Previous versions

val() should handle both cases.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement