Created
January 20, 2016 23:12
-
-
Save thegitfather/4b00f365bf6639c9634e to your computer and use it in GitHub Desktop.
randomize dom elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment