Guillermo Gutiérrez
Guillermo Gutiérrez

Reputation: 17849

Does detach in JQuery destroys the node in DOM?

Does the detach method in JQuery actually destroys the node in DOM? For example, if I do:

$("#myDiv").detach().appendTo($("#container"));

Will destroy the original #myDiv node, and create a new node to append in #container? Or it will be the same node?

With destroy, I mean that the memory location that stores the node within the browser is freed when I execute detach. Or JQuery does save a reference (not a copy) of the node, preventing it to be freed, and be available to re-attach later?

Upvotes: 2

Views: 173

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382344

The $("#myDiv") object keeps as property 0 the relevant element, even after you detached it. When you append, you really append the same element, it's not a new one.

You can check that by noticing that child elements are also restored again. Note that the remove function has the same behavior expect it cleans jQuery data linked to the element and its childs. You can also see your element(s) by issuing console.dir($("#myDiv").detach()).

Upvotes: 2

Related Questions