Daddy Warbox
Daddy Warbox

Reputation: 4598

jQuery question: Does using .remove() also properly remove children?

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?

Upvotes: 1

Views: 2425

Answers (2)

usoban
usoban

Reputation: 5478

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

Upvotes: 0

cletus
cletus

Reputation: 625097

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.

Upvotes: 4

Related Questions