I have the folowing selector
var likeComperssionOption = $('select[id*=ComparisionType]').eq(0) .find("option:contains('LIKE')");
this checks for an option which contains the word 'like' right?
How do i find an option which is exactly with the word 'like'?
Somthing like this:
var likeComperssionOption = $('select[id*=ComparisionType]').eq(0) .find("option:equals('LIKE')");
jQuery :contains() SelectorThe :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).
To select a span containing a specific text value using jQuery, we can select all the spans and then use the filter method to find the one with the given text value. We call $(“span”) to get all the spans. Then we spread the spans into an array with the spread operator.
To check if a div element contains specific text:Use the textContent property on the element to get the text content of the element and its descendants. Use the includes() method to check if the specific text is contained in the div . If it is, the includes() method returns true , otherwise false is returned.
In this case, we will use the includes() method which determines whether a string contains the specified word or a substring. If the word or substring is present in the given string, the includes() method returns true; otherwise, it returns false. The includes() method is case sensitive.
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" });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With