jquery random selector

/ Published in: jQuery
Save to your folder(s)

Copy this code and paste it in your HTML
  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);
  6. // use element
  7. $(randomElement).doSomething();

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.