I have an unordered list with list items. I have a class called 'active', which I want randomly be added on a list item. I now have this:
// I want this class added on a random list item
$("#gallery li:first-child").addClass("on");
Math.random?
var random = Math.floor(Math.random() * 1000);
var $li = $("#gallery li");
$li.eq(random % $li.length).addClass("on");
Try something like below,
var $li = $('#gallery li');
$li.eq(Math.floor(Math.random()*$li.length)).addClass("on");
Edit: Updated to avoid re-query DOM for li
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With