Assassinbeast Assassinbeast - 9 months ago 23
Javascript Question

Can you prevent autoremove JQuery Data when you remove the element from the DOM?

Is it possible to prevent auto removal of JQuery data when removing the element from the DOM?

When i for example set some data on a JQuery object for example:

$("#x").data("name", "assassinbeast");

And if I remove it from the DOM and append it to the DOM again, then all the data is gone.


You can use detach() instead of remove().

The .detach() 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.(Taken from here)

var x = $("#x").data("name", "assassinbeast");

<script src=""></script>
<div id="x" data-name="dsdsdsdsd">Element</div>