Petr Schukin
Petr Schukin

Reputation: 227

How to remove EventListener from an element (like chrome does)

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? enter image description here

Upvotes: 0

Views: 181

Answers (2)

JozeV
JozeV

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

Sxribe
Sxribe

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

Related Questions