02-26-2020 06:17 - edited 02-26-2020 13:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-26-2020 06:17 - edited 02-26-2020 13:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
How can i remove an event listener (specifically the onclick event) from a button? The onclick event was set using button.onclick = _function;
I have tried button.removeEventListener("onclick", _function); to no avail. I have also tried button.onclick = undefined; but that, instead of clearing the listener, keeps the listener but sets the behavior to undefined (meaning the memory the listener was taking isn't cleared). My app has a lot of buttons, so I want to add and remove the event listeners dynamically for the buttons to save RAM.
Answered! Go to the Best Answer.

Accepted Solutions
02-26-2020 13:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-26-2020 13:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
I figured out the answer to this. Instead of
button.onclick = _function;
I needed
button.addEventListener("click", _function);
I can now remove the even listener with
button.removeEventListener("click", _function);
02-26-2020 13:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-26-2020 13:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
I figured out the answer to this. Instead of
button.onclick = _function;
I needed
button.addEventListener("click", _function);
I can now remove the even listener with
button.removeEventListener("click", _function);
