I'm working on some project of my own where I have the following problem: - I want to change the span's content font color to be red.
Here is the HTML
<html>
<head>
<title>Testing</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script>
var arr = document.getElementsByClassName("change");
console.log(arr);
console.log(arr.length);
var i;
for (i = 0; i <= arr.length; i++) {
console.log(arr[i]);
arr[i].style.color = red;
}
</script>
</head>
<body>
<div>
<p id="test">Something <span class="change">here</span></p>
<p id="test">Something <span class="change">there</span></p>
<p id="test">Something <span class="change">everywhere</span></p>
</div>
</body>
</html>
Based on what I think, this should work. However, it's strange. on the console.log(arr) I get input of HTML Collection [ ] in the browser's console, which seems to hold all the elements just fine.
However, using arr.length gives me 0? I'm baffled as to where I'm making a mistake, and I'm sure it's something simple that I cannot put my finger on.
Help is appreciated. Thanks in advance.