Created
July 26, 2012 12:21
-
-
Save iMoses/3181731 to your computer and use it in GitHub Desktop.
jQuery Plugin wrapInGroups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Takes a jQuery collection and wraps each 'groupSize' items with 'wrapHtml' | |
* Example: $('#Example span').wrapInGroups('<div/>', 4); | |
* Will wrap every 4 span's inside #Example with a div | |
* See for yourself: http://jsfiddle.net/M9ZFh/ | |
* Author: iMoses ([email protected]) | |
*/ | |
(function($) { | |
$.fn.wrapInGroups = function(wrapHtml, groupSize) { | |
var selector = this.selector; | |
groupSize = groupSize || 1; | |
this.filter(':nth-child(' + groupSize + 'n+1)').each(function() { | |
$(this).nextAll(selector + ':lt(' + (groupSize - 1) + ')').andSelf().wrapAll(wrapHtml); | |
}); | |
}; | |
})(jQuery); |
Found this very helpful, thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work. Thank you.