thegitfather
1/20/2016 - 11:12 PM

randomize dom elements

randomize dom elements

/* Usage:

$('button').click(function() {
  $("div.band").randomize("div.member");
});

*/


(function($) {

  $.fn.randomize = function(childElem) {
    return this.each(function() {
      var $this = $(this);
      var elems = $this.children(childElem);

      elems.sort(function() { return (Math.round(Math.random())-0.5); });

      $this.remove(childElem);

      for(var i=0; i < elems.length; i++)
        $this.append(elems[i]);

    });    
  }
  
})(jQuery);