empty, remove, detach. What is the difference ?

jQuery provides various methods to remove elements from DOM. But they all are different from each other, Here’s a tiny bit of explanation.

empty() :
-This method is used to remove all the child elements from matched elements.

remove() :
-This method is used to remove all the matched element.
-This method will remove all the jQuery data associated with the matched element.

detach() : 
-This method is same as .remove() method except that the .detach() method doesn’t remove jQuery data associated with the matched elements.

remove() is faster than .empty() or .detach() method.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.