views:

507

answers:

2

Not that it matters strictly, and maybe I just don't yet fully understand how the DOM works by asking this, but I'm just trying to anticipate if there is some kind of memory leak potential here. If I remove an element that has children, event listeners, etc., do those get cleaned up as well? Or would I be wise to implement some kind of recursive removal solution myself?

To extend this question, I'll also ask: Does removing elements from the DOM directly (not via. jQuery, I mean) also have the same problem?

+4  A: 

Yes, it does. jQuery is just a wrapper for Javascript functionality that behaves int he same way: Removing a node is essentially removing a whole subtree so that includes all descendant nodes. This includes listeners (meaning anything listening to the node or one of its descendants). You can't listen to something that is no longer there.

cletus
A: 

Simple: jQuery.remove() removes children. Don't know about listeners, but probably they're removed too.

usoban