Skip to content

Instantly share code, notes, and snippets.

@szaranger
Created August 16, 2022 23:58
Show Gist options
  • Save szaranger/f2923765914106a883ba187b2054818a to your computer and use it in GitHub Desktop.
Save szaranger/f2923765914106a883ba187b2054818a to your computer and use it in GitHub Desktop.
const element = document.getElementById('button');
function onClick(event) {
element.innerHtml = 'text';
}
element.addEventListener('click', onClick);
// Do stuff
// ...
// Remove handlers
element.removeEventListener('click', onClick);
element.parentNode.removeChild(element);
// Now when element goes out of scope, both element and
// onClick will be collected even in old browsers that
// don't handle cycles well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment