Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 221
Nicolas Le Thierry d'Ennequin Avatar asked Apr 24 '13 14:04

Nicolas Le Thierry d'Ennequin


1 Answers

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

like image 112
Kevin B Avatar answered Oct 15 '22 04:10

Kevin B