I want to have closing button in some sections in my web.
what js code do i need so that i can select all the divs with the class "close" and all of them do their jobs
i tried this but it only applies for the first element.
and when i change it to querySelectorAll i get the error that this is not a function.
let close = document.querySelector(".close");
close.addEventListener("click",function () {
alert("msg");
})
2
Answers
When you use
querySelectorAll
it returns an array of HTML elements, if you want to add a listener to all of them you could iterate over the array and add a listener to each elementYou could set up a global action handler. Whenever you perform an action, get the action from the button and find its section.
Now, you can toggle the state of the section, based on the action type.