Categories
JavaScript Answers

How to remove event listener with JavaScript?

Spread the love

Sometimes, we want to remove event listener with JavaScript.

In this article, we’ll look at how to remove event listener with JavaScript.

How to remove event listener with JavaScript?

To remove event listener with JavaScript, we can use the removeListener method.

For instance, we write

let clickCount = 0;

const onClick = (event) => {
  clickCount++;
  if (clickCount == 50) {
    canvas.removeEventListener("click", onClick);
  }
};

canvas.addEventListener("click", onClick);

to call removeListener with 'click' and onClick to remove the click listener.

We add the click listener by calling addEventListener with 'click' and onClick.

Conclusion

To remove event listener with JavaScript, we can use the removeListener method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.