List of only the elements that contains
I was toying with the 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.
has method to rescue
jQuery has 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]
Final result
a = $(":contains('USA')");
b = $('*').has(":contains('USA')");
c = a.not(b) ;
console.log(c);
Above query results in following items.
[a, a]