I would like to add classes to an element in order from an array. I have three different classes, and I am not sure how to loop back to the beginning of the array to start over when I get to the end of the array of classes. Here is what I have so far:
var backgrounds = ["gray", "red", "blue"];
var elements = document.getElementsByClassName("blogpost");
var x = 0;
for (i = 0; i < elements.length; i++) {
elements[i].classname += backgrounds[i]; //This is where I do not know how to add the elements in order
x++; //This will get to the end of the array, how do I loop back to the beginning of the array after hitting the last element?
}
x = ++x % backgrounds.lengthwill reset it, though you needbackgrounds[x]instead ofbackgrounds[i]i%backgrounds.lengthand disregardxall together. i is also incrementing