tags:

views:

775

answers:

2

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?

A: 

The way I read it, these would be equivalent approaches:

From documentation for detach():

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.

From documentation for clone():

.clone( [ withDataAndEvents ] )

withDataAndEvents A Boolean indicating whether event handlers should be copied along with the elements. As of jQuery 1.4 element data will be copied as well.

Drew Wills
+1  A: 

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.

aefxx
Updated the question to reflect better what I'm trying to do. Didn't mean to lose the reference. Wanted to know if the reference contains the same data in both cases
Ron Harlev