Node.cloneNode

  • Revision slug: DOM/Node.cloneNode
  • Revision title: Node.cloneNode
  • Revision id: 25496
  • Created:
  • Creator: Celelibi
  • Is current revision? No
  • Comment

Revision Content

{{template.DomRef()}}

Summary

Returns a duplicate of the current node.

Syntax

dupNode = element.cloneNode(deep);
  • deep is a required boolean value indicating whether the clone is a deep clone or not (see notes below).

Example

p = document.getElementById("para1"); 
p_prime = p.cloneNode(true);

Notes

Cloning a node copies all of its attributes and their values.

The duplicate node returned by cloneNode is not part of the document until it is added to another node that is part of the document using appendChild or a similar method. It also has no parent until it is appended to another node.

If deep is set to false, none of the child nodes are cloned. Any text that the node contains is not cloned either as it is contained in one or more child Text nodes.

If deep evaluates to true, the whole subtree (including text that may be in child Text nodes) is copied too. For empty nodes (e.g. IMG and INPUT elements) it doesn't matter whether deep is set to true or false but you still have to provide a value.

Specification

DOM Level 2 Core: cloneNode

{{ wiki.languages( { "pl": "pl/DOM/element.cloneNode", "fr": "fr/DOM/element.cloneNode" } ) }}

Revision Source

<p>
{{template.DomRef()}}
</p>
<h3 name="Summary"> Summary </h3>
<p>Returns a duplicate of the current node. 
</p>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval"><i>dupNode</i> = <i>element</i>.cloneNode(<i>deep</i>);
</pre>
<ul><li> <code>deep</code> is a required boolean value indicating whether the clone is a deep clone or not (see <a href="#Notes">notes</a> below).
</li></ul>
<h3 name="Example"> Example </h3>
<pre>p = document.getElementById("para1"); 
p_prime = p.cloneNode(true);
</pre>
<h3 name="Notes"> Notes </h3>
<p>Cloning a node copies all of its attributes and their values.
</p><p>The duplicate node returned by <code>cloneNode</code> is not part of the document until it is added to another node that is part of the document using <a href="en/DOM/element.appendChild">appendChild</a> or a similar method.  It also has no parent until it is appended to another node.
</p><p>If <code>deep</code> is set to false, none of the child nodes are cloned.  Any text that the node contains is not cloned either as it is contained in one or more child <code>Text</code> nodes.
</p><p>If <code>deep</code> evaluates to true, the whole subtree (including text that may be in child Text nodes) is copied too. For empty nodes (e.g. IMG and INPUT elements) it doesn't matter whether <code>deep</code> is set to true or false but you still have to provide a value.
</p>
<h3 name="Specification">Specification </h3>
<p><a class="external" href="http://w3.org/TR/DOM-Level-2-Core/core.html#ID-3A0ED0A4">DOM Level 2 Core: cloneNode</a>
</p>{{ wiki.languages( { "pl": "pl/DOM/element.cloneNode", "fr": "fr/DOM/element.cloneNode" } ) }}
Revert to this revision