Skip to content

Instantly share code, notes, and snippets.

@dggodfrey
Created December 4, 2013 23:54
Show Gist options
  • Save dggodfrey/7797811 to your computer and use it in GitHub Desktop.
Save dggodfrey/7797811 to your computer and use it in GitHub Desktop.
Randomize items in the dom
//http://james.padolsey.com/javascript/shuffling-the-dom/
(function($){
$.fn.shuffle = function() {
var allElems = this.get(),
getRandom = function(max) {
return Math.floor(Math.random() * max);
},
shuffled = $.map(allElems, function(){
var random = getRandom(allElems.length),
randEl = $(allElems[random]).clone(true)[0];
allElems.splice(random, 1);
return randEl;
});
this.each(function(i){
$(this).replaceWith($(shuffled[i]));
});
return $(shuffled);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment