How to Remove Event Handlers in JavaScript ?
Last Updated :
15 Apr, 2024
In JavaScript, event handlers are functions attached to HTML elements to respond to specific events like clicks or mouse movements. Removing event handlers effectively detaches the specified function from listening to that particular event on that element.
Using removeEventListener()
This method allows us to remove event listeners that were previously added with addEventListener(). Element is the HTML element we want to remove the event listener from, eventType is a string representing the type of event (e.g., "click", "mouseover") and eventHandlerFunction is the function that was handling the event and should be removed from listening to the event.
Example 1: The below example uses removeEventListener() to Remove Event Handlers in JavaScript.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0" />
<title>Remove Event Listener Example</title>
<style>
h1 {
color: green;
text-align: center;
}
#content {
text-align: center;
margin: 20px;
}
#button-container {
text-align: center;
}
</style>
</head>
<body>
<h1 id="heading">GeeksforGeeks</h1>
<div id="content"></div>
<div id="button-container">
<button id="button">Click me!</button>
</div>
<script>
// Get the button element
const button = document.getElementById('button')
// Get the content div
const content = document.getElementById('content')
// Add event listener to button
button.addEventListener('click', handleClick)
// Function to handle button click
function handleClick() {
content.textContent = 'GeeksforGeeks is fun'
// Remove event listener from button
button.removeEventListener('click', handleClick)
}
</script>
</body>
</html>
Output:

Example 2: In the below example, the mousemove and click listner is added. With the click of a button, the mousemove listener is removed and hence the mouse location is not tracked further.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Mousemove Event Example</title>
<style>
h1 {
color: green;
text-align: center;
}
#content {
text-align: center;
margin-top: 20px;
}
</style>
</head>
<body>
<h1 id="heading">GeeksforGeeks</h1>
<div id="content"></div>
<div id="content">
<button id="button">Click me!</button>
</div>
<script>
// Get the content div
const content = document.getElementById('content');
// Get the button element
const button = document.getElementById('button');
// Function to handle mousemove event
function handleMouseMove(event) {
// Add content below heading when mouse moves
content.textContent =
`Mouse is at X: ${event.clientX}, Y: ${event.clientY}`;
}
// Function to handle button click
function handleClick() {
// Remove mousemove event listener when button is clicked
document.removeEventListener('mousemove', handleMouseMove);
}
// Add mousemove event listener
document.addEventListener('mousemove', handleMouseMove);
// Add click event listener to button
button.addEventListener('click', handleClick);
</script>
</body>
</html>
Output:

Using Inline Event Handlers
To remove an event handler using inline event handlers in JavaScript, we can directly set the corresponding attribute to an empty string or null. Here, onEventName can be events like onclick, onmouseover, etc. An alert is shown indicating that the button was clicked. After the button is clicked once, the inline event handler is removed by setting onclick to null. This ensures that subsequent clicks on the button won't trigger any action.
Example: The below example uses Inline Event Handlers to Remove Event Handlers in JavaScript.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Remove Inline Event Handler Example</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
h1 {
color: green;
text-align: center;
}
#myButton {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
#myButton:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<button id="myButton" onclick="handleClick()">
Click me!
</button>
<script>
// Function to handle button click
function handleClick() {
alert("Button clicked!");
// Remove inline event handler from button
document.getElementById('myButton').onclick = null;
}
</script>
</body>
</html>
Output:

Similar Reads
How to Handle JavaScript Events in HTML ? An Event is an action or occurrence recognized by the software. It can be triggered by the user or the system. Mostly Events are used on buttons, hyperlinks, hovers, page loading, etc. All this stuff gets into action(processed) with the help of event handlers. In this article, you will learn about d
3 min read
How to Handle Memory Leaks in JavaScript ? JavaScript, unlike some other programming languages, uses automatic memory management. This means the developer doesn't have to explicitly free up memory when they're done with it. However, memory leaks can still happen in JavaScript if objects are not properly cleaned up. A memory leak is a situati
9 min read
How to trigger events in JavaScript ? JavaScript is a high-level, interpreted, dynamically typed client-side scripting language. While HTML is static and defines the structure of a web page, JavaScript adds interactivity and functionality to HTML elements. This interaction is facilitated through events, which are actions or occurrences
2 min read
How to Remove an Event Handler using jQuery ? In jQuery, event handlers allow developers to execute code when users interact with elements, such as clicking a button or hovering over a link. However, there are situations where you may need to remove these handlers to prevent further actions or optimize performance, which can be done using speci
2 min read
How to remove an HTML element using JavaScript ? Removing an HTML element using JavaScript involves deleting it from the DOM, typically using methods like element.remove() or parentNode.removeChild(). This approach updates the webpage dynamically, allowing for responsive interactions by removing unwanted or outdated elements based on user actions
3 min read