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]

Written byNeeraj Singh
Related blogs
- 01jquery-ujs and jquery triggerMay 11, 2012 · Neeraj Singh
- 02An inline confirmation utility powered by jQueryNovember 2, 2010 · Neeraj Singh
- 03Return false has changed in jquery 1.4.3October 25, 2010 · Neeraj Singh
- 04return false considered harmful in liveMarch 10, 2010 · Neeraj Singh