$("#selectId").find("option:contains('text')").each(function(){
if( $(this).text() == 'Text that should be matched' ) {
$(this).attr("selected","selected");
}
});
I had a similar scenario with dropdowns for country, state and city. Country had "India" as well as "British Indian Ocean Territory". The issue with :contains() is that it attempts on both matches. I did something like:
i think this will do the trick for you...
I had a similar scenario with dropdowns for country, state and city. Country had "India" as well as "British Indian Ocean Territory". The issue with
:contains()
is that it attempts on both matches. I did something like:use :contains()(link)
demo
There is a filter function on jQuery that you can use to get elements with something more specific
This is going to return all options with the "text_to_select" in the text.
Here's the code I use (it's not quite the same as the other suggestions here, and works with jQuery v1.8.3)
Hope this helps.