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?
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With