Gromish
Gromish

Reputation: 199

Event listener is removed on removing the target node?

link.addEventListener('click', clickHandler, false);

adds a listener

The way to remove it is:

link.removeEventListener('click', clickHandler, false);

If I should remove for any reason the link node with:

link.remove()

How much memory safe is? I mean, can I just remove the node, and the listener is removed with it?

I cannot find documentation about it. I need to do it in a game maybe many times.

Thank you,

Upvotes: 2

Views: 422

Answers (1)

Scott Marcus
Scott Marcus

Reputation: 65806

I mean, can I just remove the node, and the listener is removed with it?

Yes, when the DOM element is removed, any handler registrations for it are removed as well (since the handler registration is stored with the element). The handler function itself, is not.

Upvotes: 2

Related Questions