greatwanderer greatwanderer - 14 days ago 4
jQuery Question

How do I hide a parent div if the child div doesn't contain an <a> tag?

I'm trying to hide a parent div (i.e., #post) if the child div (i.e., .post-title) doesn't contain an

<a>
tag. Any insight in how to accomplish this correctly with the code I've modified?

<div id="latest">
<div id="post">
<div class="post-image post-image-1"></div>
<div id="post-content">
<div class="post-title post-title-1"></div>
<div id="post-date"></div>
</div>
</div>
<div id="post">
<div class="post-image post-image-2"></div>
<div id="post-content">
<div class="post-title post-title-2"></div>
<div id="post-date"></div>
</div>
</div>
</div>




$('.post-title').not($('a').hide($("#post"));

Answer

Firstly: dont use IDs mulitple times. Every ID should be unique :)

Secondly: $('.post').not( ':has(".post-title a")' ).addClass('hide')

Comments