Reputation: 33
How to set display none (or add css style) on parent div, if it doesn´t contain any child div? Child div is dynamically attached by system, so we need to set a css class on parent, when is no child inside. Any jquery idea?
Upvotes: 2
Views: 4204
Reputation: 24945
You don't need jQuery
/ javascript
/ angular
or anything else
CSS3
's :empty
pseudo selector can help
.displayNoneWhenEmpty:empty{
display:none;
}
<div class="displayNoneWhenEmpty">Not Empty</div>
<div class="displayNoneWhenEmpty"></div>
Inspect
after running the script, and see that style
applies to empty div
Upvotes: 11
Reputation: 10177
I am assuming following html
HTML
<div class="parent">
<div class="child">
</div>
</div>
Use this jquery code
Jquery
if($('.parent').is(':empty')){
$('.parent').hide();
}
Upvotes: 0
Reputation: 392
Set a display: none;
by default to your parent. And when you dynamically attach the child, you set display: block
Upvotes: 0