Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wrap sibling groups using jQuery.
$(document).ready(function() {
$(':not(.sibling-element) + .sibling-element, * > .sibling-element:first-of-type').
each(function() {
$(this).
nextUntil(':not(.sibling-element)').
addBack().
wrapAll('<div class="sibling-wrapper" />');
});
});
<div class="entry-content">
<div class="sibling-wrapper">
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
</div>
<p>A random separator</p>
<div class="sibling-wrapper">
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
</div>
<p>Another random separator</p>
<div class="sibling-wrapper">
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
</div>
</div>
<div class="entry-content">
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
<p>A random separator</p>
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
<p>Another random separator</p>
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
<div class="sibling-element">Sibling</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment