JQuery selector - Mapping element content

Is there any way - any jQuery selector (I did not find it at http://api.jquery.com/category/selectors/ ) that can be used as an exact match?

: contains () is almost what I need, but not really. ": contains" a search in each element of the regular expression as follows:

.*<query>.* 

This means that if I need to find a link that looks something like this:

 <a href="#">Baxter</a> 

And use this:

 $("a:contains('Baxter')") 

It also matches this, which I don't want to match:

 <a href="#">I'm Peter Baxter, how are you?</a> 

I know that I can just take all the elements and compare their contents in a loop, but I want to be sure that there is no simpler way.

+8
javascript jquery
source share
1 answer

No, but it would be trivial to add as a plugin:

 $.expr[':'].contentIs = function(el, idx, meta) { return $(el).text() === meta[3]; }; 

Then you can use this as $('a:contentIs("Baxter")')

+12
source share

Source: https://habr.com/ru/post/650994/


All Articles