I need to loop through an array of 3 class elements and change the background of the second one that has the class using pure Javascript.
Here's code that I'm using but it's applying red to all 3 blocks regardless instead of only the second:
var x = document.getElementsByClassName("newClass");
var i;
for (i = 0; i < x.length; i++) {
if (x[1]) {
x[i].style.backgroundColor = "red";
} else {
x[i].style.backgroundColor = "yellow";
}
}
Is there an easier way of doing this with pure Javascript.
I'd appreciate any help with this.