Skip to content

Instantly share code, notes, and snippets.

@timothyjensen
Last active November 3, 2020 10:01
Show Gist options
  • Save timothyjensen/260931e9ac8482ba997d3ff88e6b1187 to your computer and use it in GitHub Desktop.
Save timothyjensen/260931e9ac8482ba997d3ff88e6b1187 to your computer and use it in GitHub Desktop.
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