I have a snippet of consistent mark-up that I would like to .wrapAll
with a class. I am trying to identify the <h2>
and the next two elements.
The elements that come before this snippet and after isn't consistent so I can't use .next
to the next <h2>
etc., but the .UniqueClass
on the <h2>
is something I think I can key on?
I've tried the following but it is not working.
How would I wrap the <h2>
, <p>
, and div.accordion
in one class?
var $set = $('h2.uniqueClass').children();
for (var i = 0, len = $set.length; i < len; i += 2) {
$set.slice(i, i + 2).wrapAll('<div class="test"/>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 class "uniqueClass">Headline</h2>
<p>A p tag always follows.</p>
<div class="accordion">An accordion always comes after the p tag.</div>