April 12, 2010
I was toying with simple list filter plugin and ended up with this markup.
<div id="lab">
<ul id="list">
<li><a href="">USA</a></li>
<ul>
<p>
<a href=''>USA</a>
</p>
</div>
I want to get all links that contains the word USA
. Simple enough. jQuery
supports contains
selector.
$(":contains('USA')");
Above query results in following items.
[html, body#body, div#lab, ul#list, li, a, ul, p, a]
That is because contains looks for given string under all the descendants.
jQuery has method which returns the list of elements which have a descendant which has the given string.
b = $("*").has(":contains('USA')");
Above query results in following items.
[html, body#body, div#lab, ul#list, li, ul, p]
a = $(":contains('USA')");
b = $("*").has(":contains('USA')");
c = a.not(b);
console.log(c);
Above query results in following items.
[a, a]
If this blog was helpful, check out our full blog archive.