Reputation: 227
How can remove EventListener from an element using javascript? I want to approach the same thing as chrome when we click "remove listener", but there is a problem, there are no listeners neither on document nor on element, but how chrome find and remove them?
Upvotes: 0
Views: 181
Reputation: 696
Simple example:
const someElement = document.querySelector(".someClass");
// add event listener
someElement.addEventListener("click", () => {
// log this on click event
console.log("Event added");
});
// remove event listener
someElement.removeEventListener("click", () => {
// log this when you remove event
console.log("Event removed");
});
Upvotes: 1
Reputation: 829
You can assign an event listener by calling target.addEventListener(type, handler)
You can remove this by calling target.removeEventListener(type, listener)
Upvotes: 2