Shuffle Children (jQuery)
(function($){
$.fn.shuffleChildren = function() {
var $toShuffle = this.get();
$.each($toShuffle, function(index, el) {
var $find = $(el).children(),
$findArr = $.makeArray($find),
newHTML = "";
$findArr.sort(function() { return 0.5 - Math.random() });
$.each($findArr, function(index, item) {
newHTML += "<" + item.tagName + ">";
newHTML += item.innerHTML;
newHTML += "</" + item.tagName + ">";
});
$(el).html(newHTML);
});
};
})(jQuery);
$("#shuffle").click(function() {
// Usage
$(".shuffle").shuffleChildren();
});
I doubt this is the most efficient way. (this I'm sure is far smarter). But I wrote it up for specifically so you could target a parent and shuffle it's children rather than target the children (of any type). quick demo
Comments are only visible to Forrst members. Log in or Request an invite.
