/ Published in: jQuery
A simple way of displaying a random list item. Could be used to randomly display anything.
Demo: jsfiddle.net/uRd6N/ (keep clicking run)
Demo: jsfiddle.net/uRd6N/ (keep clicking run)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
HTML: <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> <li>Item 6</li> </ul> </code> jQuery: $(function(){ var list = $("ul li").toArray(); var elemlength = list.length; var randomnum = Math.floor(Math.random()*elemlength); var randomitem = list[randomnum]; $(randomitem).css("display", "block"); });
URL: http://forrst.com/posts/Pick_a_random_element_with_jQuery-0hS