My sample HTML markup is below. In each of the three nested sets of divs, I’m removing all <p>
tags except for the first <p>
.
But now I find I want to not remove all the <p>
tags from just the first nested divs that have the anchor link a name="123"
How do I use an if
statement and the name="123"
attribute to exclude the first set of nested divs from having all their <p>
tags removed?
This jQuery doesn’t work:
$('.post-body').each(function() {
const $this = $(this);
if ($(this).closest('[name="123"]').length !== 0) {
$(this).find('p').not(":first").remove();
}
});
HTML:
<div class="post"><a name="123"></a>
<h3 class="post-title">Post</h3>
<div class="NewPermalinkUrl">Text</div> // I want to exclude this block
<div class="post-body">
<p>Content</p>
</div>
</div>
<div class="post"><a name="456"></a>
<h3 class="post-title">Post</h3>
<div class="NewPermalinkUrl">Text</div>
<div class="post-body">
<p>Content</p>
</div>
</div>
<div class="post"><a name="789"></a>
<h3 class="post-title">Post</h3>
<div class="NewPermalinkUrl">Text</div>
<div class="post-body">
<p>Content</p>
</div>
</div>