Does jQuery remove event listeners from destroyed DOM content?

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

Answers (1)

Kevin B
Kevin B

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

Related Questions