querySelectorAll() not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

querySelectorAll() not working

I'm trying to give sections a class of 'your-active-class' which has a style on CSS. So i want to give these sections that class so when a user hovers on the section, it looks active but querySelectorAll() is not working as well as getElementsByTagName() so i had to use querySelector()but it only selects the first section //Add active class to the active section let activeSection = document.getElementsByClassName("my-section").length; document.querySelector(".my-section").addEventListener("mouseover", function mouseOver() { document.querySelector(".my-section").classList.add('your-active-class'); } ); //Remove activeness let inActiveSection = document.querySelector('.my-section').addEventListener("mouseout", function mouseOut() { document.querySelector('.my-section').classList.remove('your-active-class'); } );

30th Apr 2020, 2:29 PM
Marina Khamis
Marina Khamis  - avatar
1 Answer
0
I also tried this but it only selected the first sction only . let elementsArray = document.querySelectorAll(".my-section"); elementsArray.forEach(function(elem) { elem.addEventListener("mouseover", function() { document.querySelector(".my-section").classList.add('your-active-class'); }); });
30th Apr 2020, 3:12 PM
Marina Khamis
Marina Khamis  - avatar