Skip to content

Instantly share code, notes, and snippets.

@maxxcrawford
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxxcrawford/63e9cc1fc208bc04002f to your computer and use it in GitHub Desktop.
Save maxxcrawford/63e9cc1fc208bc04002f to your computer and use it in GitHub Desktop.
Randomize Child Elements
$.fn.randomize = function(selector, callback) {
var $elems = selector ? $(this).find(selector) : $(this).children(),
$parents = $elems.parent();
$parents.each(function() {
$(this).children(selector).sort(function() {
return Math.round(Math.random()) - 0.5;
// }). remove().appendTo(this); // 2014-05-24: Removed `random` but leaving for reference. See notes under 'ANOTHER EDIT'
}).detach().appendTo(this);
});
callback && callback();
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment