2 out of 7 rated this helpful - Rate this topic

cloneNode method

[This documentation is preliminary and is subject to change.]

Copies a reference to the object from the document hierarchy.

Document Object Model (DOM) Level 3 Core Specification, Section 1.4

Syntax

object.cloneNode(fDeep)

Standards information

Parameters

fDeep [in, optional]

Type: VARIANT_BOOL

Boolean that specifies one of the following values:

VARIANT_FALSE (FALSE)

Cloned objects do not include childNodes.

VARIANT_TRUE (TRUE)

Cloned objects include childNodes.

Return value

Type: ObjectReturns a reference to the newly created node.

Remarks

The cloneNode method copies an object, attributes, and, if specified, the childNodes.

When you refer to the ID of a cloned element, a collection is returned.

cloneNodedoes not work on an IFRAME directly. You must call cloneNodethrough the all collection. The following example demonstrates how to call cloneNode on an IFRAME.


<HTML>
<SCRIPT>
function fnBegin(){
    var fr = document.all.oFrame.cloneNode();
    console.log(document.body.innerHTML);
}    
</SCRIPT>
<BODY onload="fnBegin()">
    <IFRAME id="oFrame" src="about:blank" 
        style="border:1px solid black; position:absolute; top:20px; left:30px;
            width:350px; height:300px;"></IFRAME>
</BODY>    
</HTML>

If the object being cloned is an element and that element has expandos defined on it, the expandos are copied to the clone when cloneNode is called. Other browsers might handle this differently.

In Microsoft Internet Explorer 6, this method applies to the attribute object.

Examples


<SCRIPT>
function fnClone(){
   /* the 'true' possible value specifies to clone
      the childNodes as well.
   */
   var oCloneNode = oList.cloneNode(true);
   /* When the cloned node is added,
   'oList' becomes a collection.
   */
   document.body.insertBefore(oCloneNode);
}
</SCRIPT>
<UL ID="oList">
<LI>List node 1
<LI>List node 2
<LI>List node 3
<LI>List node 4
</UL>
<INPUT type="button" value="Clone List" onclick="fnClone()">

See also

a
abbr
acronym
address
applet
area
attribute
article
aside
b
base
baseFont
bdo
bgSound
big
blockQuote
body
br
button
caption
center
cite
code
col
colGroup
comment
dd
del
dfn
dir
div
dl
documentType
dt
em
embed
fieldSet
figcaption
figure
font
footer
form
frame
frameSet
head
header
hgroup
hn
hr
html
i
iframe
img
input type=button
input type=checkbox
input type=file
input type=hidden
input type=image
input type=password
input type=radio
input type=reset
input type=submit
input type=text
ins
kbd
label
legend
li
link
listing
map
mark
marquee
menu
nav
nextID
object
ol
option
p
plainText
pre
ProcessingInstruction
q
s
samp
script
section
select
small
span
strike
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
title
tr
tt
u
ul
var
xmp
Reference
appendChild
removeNode
replaceNode
swapNode

 

 

Build date: 2/14/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Pulling my hair out
Hi there,

Is there anyway to clone an object from a current document to a newly created window document?
When ever I try I get "No such interface supported" however can clone the same object to it's original document.

This of course works in Firefox.

Thanks

[tfl - 01 10 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
SQL Server : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
Windows : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.windows%2C&
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&

Scenario that deep cloning does not operate correctly.

Heres is an example of the childnodes of the <object> tag not being cloned correctly.

***************************************************************************************

<div id="test">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="369" height="113">
<param name="movie" value="http://www.adobe.com/swf/software/flash/about/mini_FMA_about_01.swf">
<param name="quality" value="high">
<embed src="flash.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="369" height="113"></embed>
</object>
</div>
<div id="clonetest">

</div>
<script language="javascript" type="text/javascript">
clone=document.getElementById('test').cloneNode(true);

// Thanks Timothy Madden
// if(document.getElementById('test').innerHTML && clone.innerHTML != document.getElementById('test').innerHTML){
// clone.innerHTML = document.getElementById('test').innerHTML;
// }
document.getElementById('clonetest').appendChild(clone);
</script>

****************************************************************************************

Inspection using the IE Developer tools you will see that the clonetest div contains an object but does not have any parameters associated with it.

If you were to uncomment out the section in the javascript, the entire object will be properly cloned.

Hope this Help.

-Ty

Issues cloning nodes... then attempting to change the name/type attribute
Before IE8, cloning a node would clone the node, but changing the "name" or "type" attributes is not possible as they are readonly before IE8.

Thus previous to IE8 cloning form elements was of little use due to this limitation.
You might need some tricks for deep-cloning (with bCloneChildren)
From my experience deep-cloning an <option> element will only clone the start and end tags of the option, without the content.
So I suggest you do
var new_option =source_option.cloneNode(true);
if (source_option.innerHTML && new_option.innerHTML != source_option.innerHTML)
new_option.innerHTML = source_option.innerHTML;

every time you want to deep clone (that is bCloneChildren) such an element. This way if it works it works, if not fall back to using innerHTML.

-- In my tests, both IE6 or IE7 perform as expected with cloneNode(true). Timothy, can you provide a scenario that will reproduce the behavior you describe? I will be happy to alert the product team if there is something we can fix. (jsudds)