Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector - Match content of elements

Is there any way - any jQuery selector (i did found none on http://api.jquery.com/category/selectors/ ), which can be used as exact match?

:contains() is almost what i need, but not exactly. ":contains" searches in each element regex like this:

.*<query>.*

Which means, if i need to find link, which looks exactly like this:

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

And use this:

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

It matches also 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 elements and compare their content in the cycle, but I want to be sure, there's no easier way.

like image 258
Radek Simko Avatar asked Jan 12 '11 20:01

Radek Simko


Video Answer


1 Answers

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

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

You can then use this as $('a:contentIs("Baxter")')

like image 73
lonesomeday Avatar answered Sep 21 '22 00:09

lonesomeday