Alex Povar
Alex Povar

Reputation: 4960

Should I delete handler while removing element?

I'm working just few days with jQuery, so question might be not very interesting. I have DIV reloaded on button press over ajax. DIV content itself registers click handler for button. So the question is: should I remove handler which loaded content had registered?

I make little experiment and looks like I shouldn't. But is it really true?

Upvotes: 0

Views: 83

Answers (1)

Felix Kling
Felix Kling

Reputation: 816262

If elements are removed from the DOM and you have no further references to them, the event handlers will eventually be garbage collected as well.

But if you have a reference, like

var someElement = document.getElementById('someId')

then even if the element is removed by your update process, someElement will still hold a reference to that element. This also means that the event handlers still exist in memory.

Upvotes: 1

Related Questions