HTML:
<div class="div1">
<div class="child1">Some text</div>
<div class="child2">Some text</div>
<div class="child3"><img src="#" /></div>
</div>
<div class="div2">...</div> <!-- I want to hide .div2 -->
CSS:
.div1 { }
.child1 { display: none }
.child2 { display: block }
.child3 { display: none }
.div2 { }
How to hide .div2
when what is left to display in .div1
is a text and not an img
?
Tried this but doesn’t work:
if ($(".div1:not(img)").length) {
$(".div2").hide(); })
3
Answers
I hope this is what you are looking for
Get the image element lenght of
div1
Get the length of the image whose parent div has
display:none
property.If both are equal then hide the other div [
div2
].Working snippet:
You can use modern CSS selector
:has(selector)
for the job: