Minh Cường
Minh Cường

Reputation: 347

JS: event listener on hidden element

Do I need to removeEventListener when I change element's style display:none?
Do events effect page performance?

Upvotes: 2

Views: 2536

Answers (1)

Marco Bonelli
Marco Bonelli

Reputation: 69477

No you don't actually need to, specially if that element is going to be displayed again, because it wouldn't be cool to add/remove the listener every single time. Event listeners are asynchronous, and only impact on performance when they are executed, so since that you're hiding that element (and the user isn't able to interact with it) your listener will not be called as long as it stays hidden, and there will not be any performance impact.

Plus: even if you were completely removing that element using parentEl.removeChild(childEl), you still wouldn't have needed to remove the listeners, because deleting an element will cause all its listeners to be removed as well.

Upvotes: 2

Related Questions