foo DOM node, knowing foo ? +6 jquery jquery-se...">

Is it possible to use jQuery to select node text by its contents?

How to choose <a href="...">foo</a> DOM node, knowing foo ?

+6
jquery jquery-selectors
source share
1 answer

You can use .filter() , for example:

 $("a").filter(function() { return $(this).text() === "foo"; }).doSomething(); 

There is also a selector :contains() , if you do not need an exact match, for example:

 $("a:contains('foo')").doSomething(); 

Instead of an exact match, this works if the text you are looking for is anywhere in the element.


Alternatively, if you want to precisely combine and do this often, create a selector for this, for example:

 $.expr[":"].textEquals = function(obj, index, meta) { return $(obj).text() === meta[3]; } 

Then you can use it anytime after that, for example:

 $("a:textEquals('foo')").doSomething(); 
+14
source share

All Articles