jQuery: split a list into sublists

by Seetpal Singh

HTML

<ul id="test">
    <li>A</li>
    <li>B</li>
    <li>C</li>
    <li>D</li>
    <li>E</li>
    <li>F</li>
    <li>G</li>
    <li>H</li>
</ul>

JavaScript

(function($) {
    $.fn.splitList = function() {
        var that = this,
            li = $('li', that),
            len = li.length,
            half = Math.round(len / 2);
        return that.each(function() {

            li.slice(0, 3).wrapAll('<ul></ul>');

        });
    };
})(jQuery);

$('#test').splitList();