Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Difference .html("") vs .empty() [duplicate]

In Jquery what is the difference between

$('#divid').html(""); 

and

$('#divid').empty(); 

Are both doing the same operating internally inside jQuery.js?

and which one is better to use.

like image 657
Jayamurugan Avatar asked Jul 09 '13 08:07

Jayamurugan


People also ask

How to remove all html in jQuery?

jQuery remove() Method The remove() method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements. Tip: To remove the elements without removing data and events, use the detach() method instead.

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 .html in jQuery?

The html() Method in jQuery is used to set or return the innerHTML content of the selected element. Syntax: It returns the content of first matched element. $(selector).html() It sets the content of matched element.

What is the difference between Remove and empty methods in jQuery?

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.


1 Answers

T think .empty() is faster. This is the jQuery source for .empty()

empty: function() {     var elem,         i = 0;      for ( ; ( elem = this[ i ] ) != null; i++ ) {         if ( elem.nodeType === 1 ) {              // Prevent memory leaks             jQuery.cleanData( getAll( elem, false ) );              // Remove any remaining nodes             elem.textContent = "";         }     }      return this; } 

And this is the jQuery .html("") source :

html: function( value ) {     return access( this, function( value ) {         var elem = this[ 0 ] || {},             i = 0,             l = this.length;          if ( value === undefined && elem.nodeType === 1 ) {             return elem.innerHTML;         }          // See if we can take a shortcut and just use innerHTML         if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&             !wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) {              value = jQuery.htmlPrefilter( value );              try {                 for ( ; i < l; i++ ) {                     elem = this[ i ] || {};                      // Remove element nodes and prevent memory leaks                     if ( elem.nodeType === 1 ) {                         jQuery.cleanData( getAll( elem, false ) );                         elem.innerHTML = value;                     }                 }                  elem = 0;              // If using innerHTML throws an exception, use the fallback method             } catch ( e ) {}         }          if ( elem ) {             this.empty().append( value );         }     }, null, value, arguments.length ); } 

It's clear, you can choose your best.

like image 133
Aldi Unanto Avatar answered Sep 20 '22 17:09

Aldi Unanto