PostureOfLearning
PostureOfLearning

Reputation: 3541

jquery get value by text (not selected) from dropdown

I would like to retrieve the value from a dropdown based on the text.

I know I could do this:

$('#RegionsFilterDropdown option:contains("item")').val()

However, this does not return an exact match. For example, if the dropdown has options "item 1", "item 2", etc. then the above code will return more than 1 result, which is not what I want.

Any suggestions?

Thanks Rob

Upvotes: 0

Views: 1707

Answers (1)

Ram
Ram

Reputation: 144659

You are looking for .filter() method.

$('#RegionsFilterDropdown option').filter(function() {
    return $(this).text() === 'item';
}).val(); // returns value of the first matched element, if any

Upvotes: 5

Related Questions