Skip to content Skip to sidebar Skip to footer

Jquery Selector, Contains To Equals

I have the folowing selector var likeComperssionOption = $('select[id*=ComparisionType]').eq(0) .find('option:contains('LIKE')'); t

Solution 1:

Just select all the options from the select and filter them by text() value:

var likeComperssionOption = $('select[id*=ComparisionType]:first option').filter(function() { return $(this).text() == "LIKE" });

Solution 2:

If you want to select based on the value attribute of the options, you need to use the attribute equals selector to select elements with a specific value for their value attribute:

var likeComperssionOption = $('select[id*=ComparisionType]').eq(0)
                           .find("option[value='LIKE']")

Otherwise, to select based on the display text of the options, use:

var likeComperssionOption = $('select[id*=ComparisionType]').eq(0)
                           .find("option").filter(function() {
                               return $(this).text() == 'LIKE';
                           });

Update: You might be having some problems with your initial selector, it looks very strange. You should probably change

$('select[id*=ComparisionType]').eq(0)

to something simple like

$('#ComparisionType')

The concept of this answer works fine, you can see it in action here.

Post a Comment for "Jquery Selector, Contains To Equals"