Here is a more useful version of Nick above:
window.WrapMatch = function(sel, count, className){ for(var i = 0; i < sel.length; i+=count) { sel.slice(i, i+count).wrapAll('<div class="'+className+'" />'); } }
You would use this as:
var ele = $('#menu > ul > li'); window.WrapMatch(ele, 5, 'new-class-name');
Window
should be replaced with the namespace Handlers, of course.
Updated: slightly better version that uses jQuery
(function($){ $.fn.wrapMatch = function(count, className) { var length = this.length; for(var i = 0; i < length ; i+=count) { this.slice(i, i+count).wrapAll('<div '+((typeof className == 'string')?'class="'+className+'"':'')+'/>'); } return this; }; })(jQuery);
Use as:
$('.list-parent li').wrapMatch(5,'newclass');
The second parameter for the shell name is optional.
Pat 18 Oct '13 at 1:35 2013-10-18 01:35
source share