Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the jquery empty function so complicated?

I looked at the jQuery source code for the .empty() function:

empty: function() {
        for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
            // Remove element nodes and prevent memory leaks
            if ( elem.nodeType === 1 ) {
                jQuery.cleanData( elem.getElementsByTagName("*") );
            }

            // Remove any remaining nodes
            while ( elem.firstChild ) {
                elem.removeChild( elem.firstChild );
            }
        }​

Couldn't it be a lot simpler with just changing the innerHTML to an empty string:

empty: function() {
        for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
                elem.innerHTML = "";
        }​

The empty docs:

Description: Remove all child nodes of the set of matched elements from the DOM.

like image 567
gdoron is supporting Monica Avatar asked Aug 05 '26 01:08

gdoron is supporting Monica


1 Answers

Just think about .data() expandos and event handlers... By just removing the DOM, you would create memory leaks every time.

like image 64
jAndy Avatar answered Aug 06 '26 15:08

jAndy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!