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 ?
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With