The jQuery selector :contains(text)
selects an element if the matching text appears directly within the selected element, in any of that element's descendants, or a combination thereof.
I want to select an element if and only if the matching text appears directly within the selected element. What should I do?
For example,
<ul>
<li id="1">
<a><img src="la.gif"></a>
happy birthday
<ul><li>radom</li></ul>
</li>
<li id="2">
<a><img src="la.gif"></a>
sad
<ul><li>happy</li></ul>
</li>
</ul>
I want to select the li
that contains the word "happy" (don't select a li
if only its descendant contains the word "happy"). Then only li#1
should be selected.
The following does NOT do what I want because it will select both li#1
and li#2
:
$('li:contains("happy")');