Created
June 30, 2014 21:30
-
-
Save MickeyKay/99746321dd496e619241 to your computer and use it in GitHub Desktop.
dividize.js
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
/** | |
* Wrap elements in a set number of container elements with specific classes. | |
* | |
* Example Usage: wrap every 3 <li> elements within a <div class="col"></div> | |
* jQuery('li').dividize(3, 'div', 'col'); | |
* | |
* @param int columnNumber Number of columns. | |
* @param string wrapperElement The wrapper container (e.g. div, span). | |
* @param string className Class to add to the wrapper containers. | |
* | |
* @return jQuery | |
*/ | |
(function($){ | |
$.fn.dividize = function( divNumber, wrapperElement, className ) { | |
var length = this.length; | |
// Calculate the number of elements per division - round up to ensure extra elements | |
var count = Math.ceil( length / divNumber, 10 ); | |
// Slice and wrap elements | |
for ( var i = 0; i < length ; i += count ) { | |
this.slice( i, i + count ).wrapAll( '<' + wrapperElement + ' class="' + className + '"/>' ); | |
} | |
return this; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment