clone(true) + remove() vs. detach() in jQuery

Ron Harlev picture Ron Harlev · Feb 16, 2010 · Viewed 11.5k times · Source

Is the usage of

e = elem.clone(true);
elem.remove();

Identical to

e = elem.detach();

If later I append it with

e.appendTo($("#someDiv"));

In jQuery 1.4?
Will the clone(true) method preserve everything using detach() does?

Answer

aefxx picture aefxx · Feb 16, 2010

Same same but different: If you just clone a node without assigning it to a variable you will lose the copied node's reference and therefore any chance to gain a hand on its event handlers and other data (not quite true but its a PITA).

EDIT
Yes, holding a reference to the cloned element you have an exact copy (mind the true param though) that can later be appended to the DOM.