Equivalence selector

Does anyone know if there is an equivalence selector in jQuery? Of course it :containsdoes, but what if we want exact matches?

My workaround is to do

$('a').filter(function() {
   return $(this).text() == myVar;
}).addClass('highlight');

But I was hoping for a much simpler way to do $('a:equals(' + myVar + ')').addClass('highlight'). Of course, I could create a selector, but I assumed that something exists in the standard library.

Greetings

+3
source share
2 answers

There is no ready-made selector for this, but if I understand it correctly, you can easily register as :contentEqualsor something else.

Here's an example of how someone implemented a regular expression filter selector.

+3
source

: jQuery ?

:

$(document).ready(function() { 
    $.extend($.expr[':'], { 
        myEquivalence: function(el) { 
            return ($(el).val() == myVar);
        } 
    }); 
}); 
+1

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


All Articles