Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Stop onclick event listener

ANSWERED

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.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

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);

View best answer in original post

Best Answer
1 REPLY 1

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);
Best Answer