I'm trying to change the background of elements with the class "heading" on mouseover.
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 problem is that the background only changes for the first element with the class "heading". I don't know how to make this work for the other elements. How can I achieve this?