Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between jQuery.fn.empty() and jQuery.fn.html('')?

Tags:

jquery

Is there any difference between

$("#header").empty()

and

$("#header").html('')

?

Also, which should I use? $("#header").empty() is more readable, but how could anything be faster than $("#header").html('')?

like image 713
Tom Lehman Avatar asked Apr 19 '11 23:04

Tom Lehman


People also ask

What is the difference between remove () and empty () methods?

empty() will empty the selection of its contents, but preserve the selection itself. remove() will empty the selection of its contents and remove the selection itself.

What is the difference between the remove () and empty () methods in jQuery?

empty() method will clear or remove the contents inside an element, including the child nodes that are nested inside the parent or the selected element. Whereas the jQuery . remove() method will remove the selected element along with its contents and other nested elements (or child elements) and its contents.

How do you make an empty HTML in jQuery?

The empty() method removes all child nodes and content from the selected elements. Note: This method does not remove the element itself, or its attributes. Tip: To remove the elements without removing data and events, use the detach() method. Tip: To remove the elements and its data and events, use the remove() method.

What is FN HTML?

2.16 <fn> The <fn> element is a footnote used to annotate text with notes that are inappropriate for inline inclusion. It is also used to indicate the source for facts or other material used in the text.

What is the difference between empty() and empty() in jQuery?

There is no functional difference between the two. Use .empty () since it is shorter (more terse) and more readable. Don't worry about the performance difference. Remember, jQuery isn't used because it runs faster than vanilla JavaScript — it's used because it's written faster.

What is the difference between empty() and remove() methods in jQuery?

The empty() method removes all child nodes from the set of matched elements. You can try to run the following code to learn how to work with jQuery empty() method: The remove(expr) method removes all matched elements from the DOM. This does not remove them from the jQuery object, allowing you to use the matched elements further.

What is the difference between jQuery and JavaScript?

JQuery is a JavaScript library. It would not have been invented had JavaScript was not there. jQuery is still dependent on JavaScript as it has to be converted to JavaScript for the browser in-built JavaScript engine to interpret and run it.

What is the use of jQuery?

jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous for its philosophy of “Write less, do more”. Please refer to the jQuery Tutorial and jQuery Examples articles for further details.


2 Answers

There is no functional difference between the two. Use .empty() since it is shorter (more terse) and more readable.

Don't worry about the performance difference. Remember, jQuery isn't used because it runs faster than vanilla JavaScript — it's used because it's written faster. Developer time is worth far more than processor time.

There's already a jsPerf to compare the relative performance: http://jsperf.com/jquery-empty-vs-html. Every single test case shows that .empty() is faster.


Straight from the jQuery source:

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 );
        }
    }

    return this;
},

html: function( value ) {
    if ( value === undefined ) {
        return this[0] && this[0].nodeType === 1 ?
            this[0].innerHTML.replace(rinlinejQuery, "") :
            null;

    // See if we can take a shortcut and just use innerHTML
    } else if ( typeof value === "string" && !rnocache.test( value ) &&
        (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
        !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {

        value = value.replace(rxhtmlTag, "<$1></$2>");

        try {
            for ( var i = 0, l = this.length; i < l; i++ ) {
                // Remove element nodes and prevent memory leaks
                if ( this[i].nodeType === 1 ) {
                    jQuery.cleanData( this[i].getElementsByTagName("*") );
                    this[i].innerHTML = value;
                }
            }

        // If using innerHTML throws an exception, use the fallback method
        } catch(e) {
            this.empty().append( value );
        }

    } else if ( jQuery.isFunction( value ) ) {
        this.each(function(i){
            var self = jQuery( this );

            self.html( value.call(this, i, self.html()) );
        });

    } else {
        this.empty().append( value );
    }

    return this;
},

.empty() doesn't have to deal with checking the different possible argument types; it can get right down to chucking out DOM elements.

like image 59
Matt Ball Avatar answered Oct 21 '22 11:10

Matt Ball


Here's the actual source code for $().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 );
        }
    }

    return this;
}

As you can see it's a bit more involved than .html('') but as Matt suggests jQuery speeds up development not necessarily execution. As the comments in the code suggest there are advantages to using .empty vs .html in terms of cleaning up the environment once DOM nodes are removed etc

like image 1
Chris Bailey Avatar answered Oct 21 '22 13:10

Chris Bailey