removeEventListener method
The removeEventListener method
allows you to remove an event handler
previously assigned via
addEventListener.
To do this, in the parameters you need
to pass the event type and the same
function that were passed when binding
the event.
Syntax
element.removeEventListener('event type', function);
Example
Let's make it so that clicking on an element triggers only once:
<input type="button" id="button" value="click me">
let button = document.querySelector('#button');
button.addEventListener('click', func); // we assign the event
function func() {
alert('!!!');
button.removeEventListener('click', func); // remove it after clicking
};
:
See also
-
the
preventDefaultmethod
that cancels a default action