I'm learning Javascript and I can't seem to figure out why the following code does not work. It will change the background of the first paragraph, but not the second one. When I check the console, it looks like the access variable is only returning one item to the array. Any suggestions? Thanks.
HTML
<p class="blue">Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
<p class = "blue">Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
CSS:
.blue{
background: blue;
}
.orange{
background:orange;
}
Javascript:
var access = document.getElementsByClassName("blue");
for(var i = 0; i<access.length; i++){
access[i].className = "orange";
}
document.querySelectorAll('.blue')instead. It doesn't return a live node list likegetElementsByClassNamedoes and you won't get that issue.