Shuffle Children

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

Comments

  1. User Avatar
    web developer
    Permalink to comment#

    this is cool I like it.

  2. User Avatar
    Nimesh Chheda
    Permalink to comment#

    Cool, nic1

  3. User Avatar
    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>

    • User Avatar
      ryan
      Permalink to comment#

      where would I place this? i am somewhat new to coding…I am using dreamweaver, and I have made a js plugin for the code, but where would I put the ‘usage’ aspect?

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag