Posted By

smashu on 07/30/11


Tagged

random jquery


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

spee66


jquery random selector


 / Published in: jQuery
 

  1. // slice(0,1) -> 1 is the number of random elements from the $(selector)
  2. randomElement = $(selector).get().sort(function(){
  3. return Math.round(Math.random())-0.5
  4. }).slice(0,1);
  5.  
  6. // use element
  7. $(randomElement).doSomething();

Report this snippet  

You need to login to post a comment.