Let\'s say I have a collection of \'items\' like so:
Use wrapAll instead of wrap.
$(".group").wrapAll('');
Documentation of wrapAll can be found at - http://api.jquery.com/wrapAll/
Other wrapping methods available can be found at - http://api.jquery.com/category/manipulation/dom-insertion-around/
EDIT:
For the complex case where there can be more than one groups, we can achieve it using wrapAll with a $.each as follows -
var group = [];
$(".item").each(
function(i, item) {
if ($(item).hasClass("group")) {
group.push(item);
}
else {
$(group).wrapAll('');
group = [];
}
}
);