I'm in the process of learning JavaScript and have come across an issue I don't understand.
I have a simple webpage I'm using for testing, and here I'm trying to use for-in loops to change the background colour of all h1 elements to red, and all h2 elements to blue.
var h1 = document.getElementsByTagName("h1");
var h2 = document.getElementsByTagName("h2");
for (i in h1) {
h1[i].style.backgroundColor = "red";
}
for (i in h2) {
h2[i].style.backgroundColor = "blue";
}
When I run this code, the background of all h1 elements turn red as intended. However, none of the h2 elements change at all.
I get an error in the console that says: TypeError: h1[i].style is undefined
I don't understand this however as the code it's referencing seems to run just fine.
If I comment out h1[i].style.backgroundColor = "red";
and refresh the page, all of the h2 elements are changed to blue.
I then get an error in the console similar to the first one: TypeError: h2[i].style is undefined
Does anybody know what's going on any why my code isn't running both these loops? It looks like it runs fine until the end of the first loop, then errors out and stops running so doesn't run the second loop.
Thanks in advance!