Struggling to get this code to work to move the .meta div to sit below the h3 in the DOM. It works, but does it twice as there are 2 instances of this code on the page, I want it to work both times, but not display two versions of the .meta div… so both dates appear….?? What am I doing wrong ?
(function($) {
$('.home_FeedsWrapper .vc_col-sm-4 .post-header .meta').each(function() {
$(this).insertAfter($('h3'));
});
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="home_FeedsWrapper">
<div class="vc_col-sm-4">
<div class="article-content-wrap">
<div class="post-header">
<span class="meta"> 23 November 2022 </span>
<h3 class="title">This is the title here</h3>
</div>
</div>
</div>
<div class="vc_col-sm-4">
<div class="article-content-wrap">
<div class="post-header">
<span class="meta">18 October 2022 </span>
<h3 class="title">This is another title that sits here</h3>
</div>
</div>
</div>
</div>
2
Answers
You should insert each date after the next
h3
like this :Consider the following.
This ensures that each header is reviewed and the elements are only moved within that header. First, we detach the Span, and then insert it after the Header3.