Nirjhar Vermani Nirjhar Vermani - 1 month ago 8
Javascript Question

Exclude <li> with specific text in <a> tag of its children

I want to exclude

<li>
with specific text in the
<a>
tag

HTML:-

<ul>
<li class ="Group Select">
<span><a>Good</a></span>
</li>
<li class ="Group NotSelect">
<span><a>Bad</a></span>
</li>
<li class ="Group NotSelect">
<span><a>What</a></span>
</li>
<li class ="Group NotSelect Hidden">
<span><a>Better</a></span>
</li>
<li class ="Group NotSelect">
<span><a>Best</a></span>
</li>
<li>
<span><a>BadDuplicate</a></span>
</li>


In The end, I want li with class Group Select or Group NotSelect but not Group NotSelect Hidden and I do not want li which has "What" in its
<a>
tag

So far

$possibleli = $('li.Group').not("[class='Group NotSelect Hidden']");


This gives me all
li's
that I need but I want to remove li that has what in its
<a>
tag

Answer

Use the :contains() selector combined with the .not function (or the not selector combined with the :contains selector) like this:

$possibleli = $('li.Group').not("[class='Group NotSelect Hidden']").not(':contains(What)');

or

$possibleli = $('li.Group:not(.Hidden):not(:contains(What))');