Fire JS Event Listener Once

Shared by
Published
Updated

Manuel Matuzović recently tweeted about a little-known parameter that will fire JS event listener once.

Check out this little-known parameter for the click addEventListener that makes it possible to only allow it to fire once.

document.querySelector('.once').addEventListener('click', () => {
  alert('once')
}, {
  once: true
});

For more details, check out the EventTarget.addEventListener() documentation on MDN web docs.

Join the conversation.

Your email address will not be published.

All comments posted on 'Fire JS Event Listener Once' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.