treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Shuffle Children

Last updated on:

Nice clean version of a ghetto one that I wrote.

$.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();

View Demo

View Comments

Comments

  1. this is cool I like it.

  2. Cool, nic1

  3. Steve
    Permalink to comment#

    … and if you want shuffle onload:

    <pre>$(window).load(function() {
    $(“.shuffle”).shuffleChildren();
    });
    </pre>

    Instead of demo page

    <pre>$(“#shuffle”).click(function() {
    $(“.shuffle”).shuffleChildren();
    });
    </pre>

Leave a Comment

Use markdown or basic HTML and be nice.