Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery empty() vs remove()

What's the difference between empty() and remove()methods in jQuery, and when we call any of these methods, the objects being created will be destroyed and memory released?

like image 554
mabuzer Avatar asked Jun 22 '10 06:06

mabuzer


People also ask

What is the difference between Remove and empty 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.

Is Empty method in jQuery?

The empty() method is an inbuilt method in jQuery which is used to remove all child nodes and its content for the selected elements. Parameter: This method does not accept any parameter. Return Value: This method returns the selected element with the specified changes made by empty() method.

What is remove 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.


1 Answers

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

Consider:

<div>     <p><strong>foo</strong></p> </div>  $('p').empty();  // --> "<div><p></p></div>"  // whereas, $('p').remove(); // --> "<div></div>" 

Both of them remove the DOM objects and should release the memory they take up, yes.


Here are links to documentation, which also contains examples:

  • .remove()
  • .empty()
like image 103
nickf Avatar answered Oct 12 '22 23:10

nickf