Skip to content

Event Listener - Once True

• 1 min

You can add the option { once: true } to an event listener to automatically remove it when has been invoked.

example: https://codepen.io/matuzo/pen/wvMOpXp?editors=1010

document.querySelector('.always').addEventListener('click', () => {
alert('always')
});

// after this fires it's removed from the dom
document.querySelector('.once').addEventListener('click', () => {
alert('once')
}, {
once: true
});