tags:

views:

27

answers:

2

Does anyone know if there's an equivalence selector in jQuery? Of course :contains exist, 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 of a much easier method of doing $('a:equals(' + myVar + ')').addClass('highlight') instead. Of course I could create the selector, but I'd have assumed something exists in the standard library.

Cheers

+3  A: 

There doesn't seem to be a pre-built selector for this but if I understand it correctly, you could register your own easily as :contentEquals or whatever.

Here is an example of how somebody implemented a regex filter selector.

Pekka
+1  A: 

You'll need your own custom selector: There's a discussion about them here http://stackoverflow.com/questions/1940574/what-useful-custom-jquery-selectors-have-you-written

Your custom selector might look something like:

$(document).ready(function() { 
    $.extend($.expr[':'], { 
        myEquivalence: function(el) { 
            return ($(el).val() == myVar);
        } 
    }); 
}); 
James Wiseman
Yup, I knew exactly this is what I was going to need as I had previously pointed out. Good to see an example though, thanks very much!
Kezzer