Skip to content

Instantly share code, notes, and snippets.

@HendrikHaase
Last active January 22, 2016 22:30
Show Gist options
  • Select an option

  • Save HendrikHaase/4d6070e1e29ca2916777 to your computer and use it in GitHub Desktop.

Select an option

Save HendrikHaase/4d6070e1e29ca2916777 to your computer and use it in GitHub Desktop.
(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);
$(elem).shuffle();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment