I'm trying to randomly set some css rules to different section tags, but I don't know how many section tags I will end up with. If there are 10 section tags, each section should have a random class name between section0
to section9
. It needs to be randomized and not in order.
I read about document.getElementByTagName
and I'm able to assign class names with an integer by using a for loop, but I'm not sure where and how to implement Math.floor(Math.random() * myArray.length)
and return it correctly.
function myFunction(){
var randomize = document.getElementsByTagName("section");
for (i = 0; i <= randomize.length; i++) {
randomize[i].className = "section" + i;
}
}
I've tried i = Math.floor(Math.random() * randomize.length);
and return randomize[i].className = "section" + i;
but that does not seem to work. I'm trying to incorporate two things I've been reading about but I'm not sure how to correctly do so.