Creating One Time Executing Events in Javascript

Published on December 8, 2020

If we need a Javascript event to execute only once, we can use the once option as parameter to the addEventListener() method.

By setting the once option to true, the event listener will be invoked only once. After that it will be automatically removed.

document.querySelector("#my-button").addEventListener('click', function() {
	// event handler
}, {once: true});
Related Tutorials
Adding Event Handlers on Dynamically Created Elements with Vanilla Javascript
Add Event to Multiple Elements By Class in Javascript
Loading Comments