I have a list of elements (divs) preseded by a H3 tag
<
You could do something like this:
(function(c, h, s, $g, n) {
$(c).find([h,s].join()).each(function() {
if ($(this).filter(h).length || $g.find(s).length == n) {
$g = $g.clone().empty().insertAfter(this);
}
$g.append($(this).not(h));
});
})(document, 'h3', '.item', $(''), 3);
If your elements are contained in a specific container, then pass the container's selector (eg. "#myContainer") instead of document.
DEMO