Reputation: 13
A lot of .cell
elements are added to the DOM on document load. Only some of them are needed. If an <img>
was also created within them, they are needed. If an <img>
element was not created within the .cell
elements, then delete the whole .cell
element. If the <img>
was created then do nothing.
How can I do that?
<div class="cell">
<div class="a-class">
<div class="always-generated-div"></div>
<img> <!-- if generated --->
</div>
</div>
jq(".cell img").each(function() {
if (jq(this).length > 0) {
console.log("Oh There You Are");
} else {
console.log("Empty elements erased");
jq( ".cell" ).remove();
}
});
Upvotes: 1
Views: 50
Reputation: 337590
You can select a parent by their child elements using the :has()
selector (and the :not()
selector in this case too). From there, you can remove()
them. Try this:
$('.cell:not(:has(img))').remove();
Upvotes: 3