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.
Best AnswerI 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);