Last Updated: February 25, 2016
·
696
· bhousman

Removing Event Listeners

It's good practice, especially in long-running web applications, to remove event listeners, such as the following:

element.removeEventListener(<event-name>, <callback>, <use-capture>);

You will need a reference to a callback function that was originally bound.

var element = document.getElementById('element');

function callback() {
  console.log('You will see my 1x time.');
  element.removeEventListener('click', callback);
}

// Add listener
element.addEventListener('click', callback);

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside