Created
May 10, 2011 16:27
-
-
Save cowboy/964822 to your computer and use it in GitHub Desktop.
jQuery Random: Add an "element of surprise" into the chain!
This file contains hidden or 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
/*! | |
* jQuery Random - v0.1 - 5/10/2011 | |
* http://benalman.com/ | |
* | |
* Copyright (c) 2011 "Cowboy" Ben Alman | |
* Dual licensed under the MIT and GPL licenses. | |
* http://benalman.com/about/license/ | |
*/ | |
(function($){ | |
$.fn.random = function() { | |
// The result set. | |
var elems = []; | |
// Select all elements currently in the DOM. | |
var all = $('*'); | |
// For each selected element... | |
return this.each(function() { | |
// Push a random element onto the result set. | |
elems.push(all[~~(Math.random() * all.length)]); | |
// And return a new set that's revertable with .end(). | |
}).pushStack(elems); | |
}; | |
})(jQuery); |
This file contains hidden or 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
// jQuery Random v0.1, 5/10/2011, http://benalman.com/ Copyright (c) 2011 "Cowboy" Ben Alman, dual licensed MIT/GPL. | |
;(function(a){a.fn.random=function(){var b=[],c=a("*");return this.each(function(){b.push(c[~~(Math.random()*c.length)])}).pushStack(b)}})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice use of the bitwise not! Didn't think to use it like that. Now I can save some chars (while probably confusing people).