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

Is used

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

Identically

 e = elem.detach(); 

If later I add it using

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

In jQuery 1.4?
Will the clone (true) method save everything with the detach () function?

+4
source share
2 answers

It’s the same, but different: if you simply clone a node without assigning it to a variable, you will lose the copied node link and, therefore, will be able to access your event handlers and other data (not quite true, but it's PITA).

EDIT
Yes, with a link to the cloned element, you have an exact copy (remember the true parameter), which can later be added to the DOM.

+2
source

As I read it, these would be equivalent approaches:

From the documentation for detach () :

The .detach () method is the same as .remove (), except that .detach () saves all jQuery data associated with deleted items. This method is useful when deleted items need to be reinserted at the DOM time.

From the documentation for clone () :

.clone ([withDataAndEvents])

withDataAndEvents Logical indication of whether event handlers should be executed copied along with the elements. Starting with jQuery 1.4 element data will be copied as well.

0
source

Source: https://habr.com/ru/post/1301470/


All Articles