Looking to dynamically change the background of elements in the "heading" class when hovered over.
const heading = document.querySelector('.heading');
const headingAll = document.querySelectorAll('.heading');
headingAll.forEach(item => {
item.addEventListener("mouseover",()=>{
heading.style.backgroundColor='teal';
});
})
.heading{
background-color: yellow;
width: 50%;
margin:0 auto;
padding: 10px;
border: 1px solid red;
cursor: pointer;
}
.heading2,heading3{
margin-top: 10px;
}
<h1 id='heading' class='heading heading2'>First Heading</h1>
<h2 id='heading' class='heading heading2'>Sub Heading</h2>
<h3 id='heading' class='heading heading3'>Third Heading</h3>
The issue is that only the first element with the class "heading" changes background on mouseover. How can I extend this functionality to work for all elements with the same class?