With this code I am looping through the li
elements to add the class 'cool'. Just testing different methods such as getElementById
and querySelectorAll
and they work fine, but when I use getElementsByClassName
, it skips every other one out, not applying the style to it. I removed the i++
counter which applies it to every element, but wouldn't that lead to an infinite loop? Can anyone explain?
var listItems = document..getElementsByClassName('test');
var i;
for (i = 0; i < listItems.length; i++) { //loop through elements //remove ++ when using .getElementsByClassName method to select each element
listItems[i].className = 'cool';// change class to cool
}