I'm having trouble getting an event listener to apply to all items in an array I've created. I've looked at lots of other related posts here, but none seem to cover my precise situation.
I know the for loop is working because I'm logging it out to the console and can see each iteration pop up. I can even see in the console that the event listener has been applied to each iteration, but won't fire when actually mousing over the element on the page.
Any clues on what I might be missing? The complete code is below.
<head>
<title>Test Page</title>
</head>
<style>
.red-text {
color: red;
font-size: 75px;
transition: 4s;
font-family: Times;
}
.new-header {
color: blue;
font-size: 100px;
transition: 3s;
font-style: bold;
}
</style>
<body>
<h1>Welcome</h1>
<p>This is the first paragraph</p>
<p>Here is the second paragraph</p>
<p>And this is the final paragraph</p>
<script>
var h = document.querySelector("h1");
var p = document.querySelectorAll("p");
var changeHeader = function() {
h.className = h.className + " new-header";
}
var addRedClass = function() {
p.className = p.className + " red-text";
}
h.addEventListener("mouseover", changeHeader);
for(var i = 0; i < p.length; i += 1) {
console.log(p[i]);
p[i].addEventListener("mouseover", addRedClass);
}
</script>
</body>
Again, each element in the array is being targeted, and I can verify that the listener is being applied, but it will not fire on mouse over.
Thanks in advance for any advice!
pmight not have dimensions via CSS in the DOM that allow it to be mouseovered perhaps.