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

Answer Posted / chaitanya

All these methods .empty(), .remove() and .detach() are used for removing elements from DOM but they all are different.

.empty(): This method removes all the child element of the matched element where remove() method removes set of matched elements from DOM.

.remove(): Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.

.detach(): This method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

Find out more here

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain deferred and promise object in jquery? : jquery mobile

598


Explain the difference between onload() and document.ready() function used in jquery?

613


How can we include jQuery library in ASP.Net project?

630


How can jquery library be added to pages?

598


What are the advantages of using cdn?

617


Define each() function in jquery?

589


What is .empty()? : jquery mobile

619


Do you have plans for a new jquery plug-in or something else we should look out for? Where to from here for the imgpreview plugin?

606


How to revert the most recent ‘destructive’ operation, changing the set of matched elements to its previous state using ?

606


Is jQuery is a client or server scripting?

647


Name 3 available jquery plugins that bootstrap has in their query plugin library.

588


Why should I use jquery?

645


What are the types of selectors that are used in jquery? Give examples.

636


What is a filter in jquery? Explain

644


Differentiate between calling stop (true, true) and finish method?

646