My content management system allows users to easily insert blocks of content onto a page. These blocks can be of different types and inserted in any order. A sample high-level DOM structure may appear like this:
<p>Some rich text</p>
<div class="box">...</div>
<div class="box">...</div>
<div class="box">...</div>
<h3>Some more rich text</h3>
<p>Lorem ipsum</p>
<div class="box">...</div>
<div class="box">...</div>
I am looking to group any adjacent 'box' divs within a wrapping 'container' div. In the given example, two 'container' divs would need to be added as there are two sets of box divs, resulting in:
<p>Some rich text</p>
<div class="container">
<div class="box">...</div>
<div class="box">...</div>
<div class="box">...</div>
</div>
<h3>Some more rich text</h3>
<p>Lorem ipsum</p>
<div class="container">
<div class="box">...</div>
<div class="box">...</div>
</div>
I don't believe this can be achieved with CSS selectors, so I'm curious if anyone knows how to accomplish this using jQuery.