Skip to main content
Home / Code Snippets / jQuery Code Snippets / Shuffle Children

Shuffle Children

$.fn.shuffleChildren = function() {
    $.each(this.get(), function(index, el) {
        var $el = $(el);
        var $find = $el.children();

        $find.sort(function() {
            return 0.5 - Math.random();
        });

        $el.empty();
        $find.appendTo($el);
    });
};

Usage

$(".parent-element").shuffleChildren();

See the Pen
jQuery Shuffle Function
by Chris Coyier (@chriscoyier)
on CodePen.

icon-link icon-logo-star icon-search icon-star