For elements that get event handlers, data, or perform certain other operations from jQuery methods, jQuery will create an entry in jQuery.cache
for that element.
The element itself gets a property which holds the key to its entry in jQuery.cache
. That's the only thing that links the element and the data.
So when an element is going to be removed, jQuery needs to look up the entry in jQuery.cache
by using the key found on the element, and then needs to destroy the related data. If it didn't do this, there would be a memory leak since once the element is gone, there's no way to know that the jQuery.cache
data should be removed.
So if you were to use the native .removeChild()
to remove and destroy an element, you would be destroying the only link to that data. That's why you must unfortunately use jQuery for all element removal, and why you shouldn't use any other DOM libraries alongside jQuery.