Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector, contains to equals

Tags:

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')"); 
like image 335
Aharon Muallem Avatar asked Sep 08 '11 06:09

Aharon Muallem


People also ask

How use contains in jQuery?

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).

How do I select a span containing a specific text value using jQuery?

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.

How do I check if a div contains a text?

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.

How do you check if a string contains another string in jQuery?

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.


2 Answers

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" }); 
like image 97
Igor Dymov Avatar answered Sep 19 '22 16:09

Igor Dymov


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.

like image 26
Jon Avatar answered Sep 19 '22 16:09

Jon