See the following markup:
<div class="careersIntegration__listing" id="careers-listing">
<div class="careersIntegration__accordion">
<div class="careersIntegration__accordion-header">
<span class="careersIntegration__accordion-dept">Sales</span>
</div>
<div class="careersIntegration__accordion-jobs" data-dept="sales"></div>
</div>
<div class="careersIntegration__accordion">
<div class="careersIntegration__accordion-header">
<span class="careersIntegration__accordion-dept">Customer Success</span>
</div>
<div class="careersIntegration__accordion-jobs" data-dept="customer-success">
<figure class="careerCard" data-dept="customer-success">Job</figure>
</div>
</div>
</div>
I'm trying to hide .careersIntegration__accordion
's which have .careersIntegration__accordion-jobs
elements that have no children.
To simplify, if .careersIntegration__accordion-jobs
has no children, hide the parent .careersIntegration__accordion
element.
I've seen examples on existing SO posts (see this one), but cannot get the approach to work in my instance. See posts below which I have tried:
- jquery if div id has children
- jQuery hide parent div if child div is empty
- css hide div if div has no child with class
With my current approach below, the else
statement is executed, unsure why?
$(".careersIntegration__accordion").each(function(){
if( $(this).children(".careersIntegration__accordion-jobs").length == 0 ){
$(this).parent().hide();
} else{
console.log('has children');
}
});