ZVON > References > DOM1 Reference

cloneNode (method)

Owning interface and usage:  
Node.cloneNode(deep)

Member of these other interfaces :  
Attr, CDATASection, CharacterData, Comment, Document, DocumentFragment, DocumentType, Element, Entity, EntityReference, Notation, ProcessingInstruction, Text

Description:  
Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes. The duplicate node has no parent (parentNode returns null).

Parameters:  
Boolean deep  -  If true, recursively clone the subtree under the specified node; if false, clone only the node itself (and its attributes, if it is an Element).

Returns:  
Node -  The duplicate node.

Exceptions:  
none

Note:  
Cloning an Element copies all attributes and their values, including those generated by the XML processor to represent defaulted attributes, but this method does not copy any text it contains unless it is a deep clone, since the text is contained in a child Text node. Cloning any other type of node simply returns a copy of this node.


Example:
Text in the first DIV.
Some text in the second DIV.
Some text and element in the third DIV.
We can try another elements. It will be much more interesting.
Text in the last DIV.
Source:
       <div id="doc">
         <div>
           Text in the first DIV.
         </div>
         <div id="DDD" class="secondClass">
           Some text in the second DIV.
         </div>
         <div class="thirdClass">
           Some text and <span id="SSS">element</span> in the third DIV.
         </div>
         <div class="fourthClass">
           We can try <i>another elements</i>.
           It will be much more <b>interesting</b>.
         </div>
         <div>
           Text in the last DIV.
         </div>
       </div>
     
JavaScript:
  var main = document.getElementById('doc');
  var elem = main.childNodes[3];
  var returns = elem.cloneNode(true);
  var output = returns.firstChild.nodeValue;
Output:
desired your browser
white-spaces

preserved

by default
Some text in the second DIV.
white-spaces

not-preserved

by default
We can try