JavaScript
Event Handling
onclick listener
Web Development
Programming Tips

Remove an onclick listener

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Removing a click listener in JavaScript is easy once you know how the listener was attached. The important distinction is whether the handler was assigned through the onclick property or registered with addEventListener, because the removal API is different for each case.

Removing a Handler Assigned to onclick

If the click behavior was added through the DOM property, remove it by setting the property back to null.

html
1<button id="saveBtn">Save</button>
2<script>
3  const button = document.getElementById("saveBtn");
4
5  button.onclick = function () {
6    console.log("clicked");
7  };
8
9  button.onclick = null;
10</script>

This works because onclick stores only one handler reference at a time.

Removing a Listener Added With addEventListener

If you used addEventListener, remove it with removeEventListener. The crucial rule is that you must pass the exact same function reference.

html
1<button id="deleteBtn">Delete</button>
2<script>
3  const button = document.getElementById("deleteBtn");
4
5  function handleClick() {
6    console.log("delete clicked");
7  }
8
9  button.addEventListener("click", handleClick);
10  button.removeEventListener("click", handleClick);
11</script>

This is the preferred pattern because it supports multiple listeners and clearer composition.

Why Anonymous Functions Cause Trouble

The following code adds a listener but does not give you a reusable reference for removal:

javascript
button.addEventListener("click", function () {
  console.log("clicked");
});

Later, this will not remove the original listener:

javascript
button.removeEventListener("click", function () {
  console.log("clicked");
});

Those are two different function objects, even though the source code looks the same.

The fix is to store the function in a variable or use a named function:

javascript
1const handleClick = () => console.log("clicked");
2
3button.addEventListener("click", handleClick);
4button.removeEventListener("click", handleClick);

Matching Listener Options

If you attached the listener with options such as capture mode, removal must match the relevant option values.

javascript
1function handleClick() {
2  console.log("capturing click");
3}
4
5button.addEventListener("click", handleClick, true);
6button.removeEventListener("click", handleClick, true);

If the add call used capturing and the remove call does not, the listener stays attached.

Event Delegation as an Alternative

In dynamic interfaces, attaching and removing many individual click listeners can become noisy. Event delegation places one listener on a parent and checks the event target.

html
1<ul id="menu">
2  <li data-action="open">Open</li>
3  <li data-action="save">Save</li>
4</ul>
5
6<script>
7  const menu = document.getElementById("menu");
8
9  function handleMenuClick(event) {
10    const item = event.target.closest("[data-action]");
11    if (!item) return;
12    console.log(item.dataset.action);
13  }
14
15  menu.addEventListener("click", handleMenuClick);
16
17  // later
18  menu.removeEventListener("click", handleMenuClick);
19</script>

This is often simpler than managing many separate item-level listeners.

Temporary Disable Versus True Removal

Sometimes you do not actually need to remove the listener permanently. If the goal is to prevent duplicate clicks during a save operation, temporarily disabling the element can be clearer than detaching and reattaching handlers.

javascript
1button.disabled = true;
2
3setTimeout(() => {
4  button.disabled = false;
5}, 1000);

That is not the same as listener removal, but it is often the more maintainable solution when the event wiring itself should remain intact.

Common Pitfalls

The most common mistake is trying to remove an addEventListener callback with a new anonymous function. Removal works only with the original function reference.

Another issue is mixing onclick and addEventListener as if they were the same mechanism. Setting onclick = null does not remove listeners that were registered with addEventListener.

A third pitfall is forgetting listener options. If the original registration used capture mode, the removal call must match that mode.

Finally, developers sometimes remove listeners from a different element than the one they were attached to. That sounds obvious, but it is a common bug in code with repeated DOM queries or rerendered nodes.

Summary

  • Remove onclick handlers by setting the property to null.
  • Remove addEventListener handlers with removeEventListener.
  • Use the exact same function reference when removing a listener.
  • Match capture options when the listener was registered with them.
  • Consider event delegation when many click handlers would otherwise be attached individually.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.