Is there a version of wrapAll that will only wrap consecutive elements? So this:
<p>foo</p>
<p>foo</p>
<h2>bar</h2>
<p>foo</p>
turns into this:
<div>
<p>foo</p>
<p>foo</p>
</div>
<h2>bar</h2>
<div>
<p>foo</p>
</div>
When this is run?
$('p').wrapAll2('<div />')
Here’s one way to do it:
or as a fully fledged plugin:
*Note that it presumes you will call it on homogeneous collections (as least all the same node type)
EDIT
Experimentally, I’ve looked into jQuery’s innards and found that it stores the selector used to create a collection! [at least on instantiation], so with that in mind I’ve created another version that at least works with the current version of jQuery, and accepts any selector!
It does not work with things like this though:
$('p').add('span').wrapAll2('<div />');