Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created February 2, 2011 20:40
Show Gist options
  • Save mindplay-dk/808383 to your computer and use it in GitHub Desktop.
Save mindplay-dk/808383 to your computer and use it in GitHub Desktop.
swap() and jumble() functions for jQuery
/**
* http://plugins.jquery.com/project/swap-jumble
*/
(function($){
$.fn.swap = function(b){
b = $(b)[0];
var a = this[0];
var t = a.parentNode.insertBefore(document.createTextNode(''), a);
b.parentNode.insertBefore(a, b);
t.parentNode.insertBefore(b, t);
t.parentNode.removeChild(t);
return this;
};
$.fn.jumble = function(){
var max = this.length;
for (var i=0; i<max*2; i++) {
var a = Math.floor(Math.random()*max);
var b = a;
while (b==a)
b = Math.floor(Math.random()*max);
$(this[a]).swap(this[b]);
}
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment