Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.clone and .cloneNode

I am a bit confused on the difference between jQuery $.clone and the raw .cloneNode property.

If I am doing

$('blah').cloneNode(true) this will create a global object outside of the jQuery space.

If I use

$('blah').clone(true) this will create a jQuery object inside the jQuery space but copy everything including events ?

If I am using jQuery should I stick with .clone and if I change my code from .cloneNode will there any effect ?

like image 715
Andy Avatar asked Feb 28 '12 05:02

Andy


People also ask

What is clone node?

Definition and Usage The cloneNode() method creates a copy of a node, and returns the clone. The cloneNode() method clones all attributes and their values. Set the deep parameter to true if you also want to clone descendants (children).

How do I clone a div?

To clone a div and change its id with JavaScript, we can use the cloneNode method on the div. Then we can set the id property of the cloned element to set the ID. to add the div.


1 Answers

A few things. You call cloneNode on this not $(this). Second, with cloneNode you can't clone the events associated with the original node, whereas with jQuery's clone, it clones the events and data (if the first flag is set). Setting the second flag of clone clones the original element's children and their elements.

Use accordingly, according to your needs.

like image 54
Paul Bruno Avatar answered Sep 20 '22 11:09

Paul Bruno