Reputation: 6154
For an Ajax application, I'm repeatedly using jQuery's html()
method to update a DOM container (overwriting HTML content, then binding elements to event listeners).
Are the event listeners that were attached to the destroyed content correctly removed by jQuery, or is there a risk of memory leaks? (There are already a few related questions out here, but I couldn't find the answer).
Upvotes: 9
Views: 1339
Reputation: 95031
Yes, all events and data are cleaned up when you remove or replace content with jQuery methods. It is done using the internal cleanData
method.
https://github.com/jquery/jquery/blob/1.9-stable/src/manipulation.js#L242 https://github.com/jquery/jquery/blob/1.9-stable/src/manipulation.js#L746
Upvotes: 8