Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right jquery selector to get all instances of a div based on content inside that div?

I have the following html:

<div class="list">
 <div class="sortable">
        <div class="contentItem"> Some content </div>
        <div class="contentItem"> Some content </div>
        <div class="contentItem"> Some content </div>
 </div>
</div>

<div class="list">
 <div class="sortable">
 </div>
</div>

<div class="list">
 <div class="sortable">
        <div class="contentItem"> Some content </div>
        <div class="contentItem"> Some content </div>
        <div class="contentItem"> Some content </div>
 </div>
 </div>

 <div class="list">
 <div class="sortable">
 </div>
 </div>

and i am trying to hide all of the "list" classes that don't have any "contentItem" divs inside of them. So its basically taking this

 $(".list").hide();   

which would hide all but ignore the ones that have "contentItem" elements in them

is this possible as a jquery selector?

like image 874
leora Avatar asked Feb 16 '14 03:02

leora


2 Answers

Use :not() and :has()

$(".list:not(:has('.contentItem'))").hide(); 

Fiddle Demo

like image 57
Tushar Gupta - curioustushar Avatar answered Oct 03 '22 09:10

Tushar Gupta - curioustushar


You can use :not() along with :has() pseudo selector to accomplish your task.

Try,

 $(".list:not(:has('.contentItem'))").hide(); 

DEMO

like image 39
Rajaprabhu Aravindasamy Avatar answered Oct 03 '22 07:10

Rajaprabhu Aravindasamy