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

How to disable cut,copy and paste in TextBox using jQuery?

477


What is event.PreventDefault in jQuery?

514


Explain .siblings() method in jquery?

8664


Can we select a element having a specific class in jquery ?

449


Does jquery follow w3c recommendations?

502






Is it possible that jquery html work for both html and xml document?

441


How do you update ajax response with id " resilts"?

529


How to disable/enable an element in jquery?

499


Difference between detach and remove? : jQuery Mobile

450


What is the jQuery code to select all links inside the paragraph?

457


Do we need to add jQuery file in both Master and Content page?

467


Difference between parent() and parents() in jQuery?

515


How to disable jquery animation? : jquery mobile

421


How to use it jquery knockout?

477


Can JQuery Run on MAC or Linux instead of Windows?

481