.removeEventListener()
The .removeEventListener()
method is an essential part of JavaScript event handling. It is used to remove an event listener from a specified element in JavaScript that was previously added to an element using .addEventListener()
. This method ensures that an event handler no longer executes when the specified event occurs on the target element.
For .removeEventListener()
to work correctly, the function reference used to remove the event listener must match the function reference used when initially attaching the event. Anonymous functions cannot be removed since they do not have a reference to match.
Syntax
EventTarget.removeEventListener(event, function, useCapture)
Parameters:
event
: The name of the event (e.g.,'click'
,'keydown'
).function
: The event handler function reference that was previously attached.useCapture
(Optional): A boolean that specifies whether the event was registered in the capturing phase. Defaults tofalse
.
Note: The function reference used in
removeEventListener()
must be the same as the one used inaddEventListener()
. If an anonymous function was used inaddEventListener()
, it cannot be removed.
Return value:
This method does not return a value (i.e., it returns undefined
).
Example 1: How to Remove a Click Event Listener in JavaScript
This example demonstrates adding and removing a click
event listener from a button element.
<!DOCTYPE html><html><head><title>removeEventListener Example</title></head><body><button id="myButton">Click Me</button><script>function handleClick() {alert('Button clicked!');}let button = document.getElementById('myButton');button.addEventListener('click', handleClick);setTimeout(() => {button.removeEventListener('click', handleClick);alert('Event listener removed');}, 5000);</script></body></html>
Example 2: How to Remove a Mouseover Event Listener in JavaScript
This example removes a mouseover
event listener from a div element after a click event.
<!DOCTYPE html><html><head><title>removeEventListener Mouseover Example</title></head><body><divid="hoverDiv"style="width: 200px; height: 100px; background: lightblue;">Hover over me</div><button id="removeBtn">Remove Mouseover Listener</button><script>function handleMouseOver() {alert('Mouse over detected');}let divElement = document.getElementById('hoverDiv');let removeBtn = document.getElementById('removeBtn');divElement.addEventListener('mouseover', handleMouseOver);removeBtn.addEventListener('click', () => {divElement.removeEventListener('mouseover', handleMouseOver);alert('Mouseover listener removed');});</script></body></html>
To level up your understanding of event handling in JavaScript, check out these Codecademy courses:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours