Home/Jquery/.empty() vs .remove() vs .detach()

.empty() vs .remove() vs .detach()

jQuery provides various methods to remove elements from DOM. These methods are

  • .empty()
  • .remove() and
  • .detach()

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

.remove(): This method used when you want to remove the element itself, as well as everything inside it.
(This method takes elements out of the  DOM)

READ  html vs text

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

Example 1:




READ  jQuery DOM Traversing

OUTPUT DOM : empty()


OUTPUT DOM : remove()


Exampke 2:

var temp = null;
temp = $(“#dvjQ”).detach();

Note : Then detach the div and attach it again  remove also do the same the difference is it also erase the event associated with the element.(hover event….)

Leave A Comment