Let\'s say I have a collection of \'items\' like so:
Try wrapAll method instead:
$(".group").wrapAll("");
DEMO: http://jsfiddle.net/LanMt/3/
For wrapping the separate groups of .group
elements you can use the following:
$(".group").map(function() {
if (!$(this).prev().hasClass("group")) {
return $(this).nextUntil(":not(.group)").andSelf();
}
}).wrap("");
DEMO: http://jsfiddle.net/LanMt/5/
The code above was assembled with the help of @Jon's answer.