I need to add HTML into a page but I have no direct control over the HTML itself so am trying to use JQuery.
This is the existing, generated HTML (simplified):
<div class="curriculum>Content</div>
<div class="curriculum">Content</div>
<div class="testimonial">Content</div>
<div class="testimonial">Content</div>
<div class="testimonial">Content</div>
<div class="curriculum>Content</div>
<div class="curriculum">Content</div>
Wherever the .testimonials
appear, I need to group them all and wrap in HTML so the output looks like:
<div class="curriculum>Content</div>
<div class="curriculum">Content</div>
<div>
<div>
<h2>Testimonials</h2>
<div>
<div class="testimonial">Content</div>
<div class="testimonial">Content</div>
<div class="testimonial">Content</div>
</div>
</div>
</div>
<div class="testimonial">Content</div>
<div class="curriculum>Content</div>
<div class="curriculum">Content</div>
Note that I need to add the 3 containing <div>
tags and the <h2>
(and the relevant closing tags) for styling reasons.
I was hoping to simply grab the first and last testimonials using .first().before
and .last().after
to add the HTML but JQuery adds the closing tags. I've also tried using JQuery's various .wrapAll
, .before
, .append
but have had no luck for reasons I don't understand!
Other solutions on stackoverflow all seem to deal with wrapping the group of elements in a single <div>
but I have trouble when trying to add the other <div>
s and the <h2>
Does anybody have any advice how this could be achieved?