cloneNode method
Returns an exact copy of the current node.
The new node contains the same attributes with the same values as the original element.
The child elements of the cloned node can also be copied in the original order with the cloneNode method (depending on the first parameter of the cloneNode method).
The new node has no parent, use the insertBefore or appendChild method to insert it into the document.
If you need to copy a node from another document into the current document, use the importNode method.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required in Firefox and Opera, optional in Internet Explorer, Google Chrome and Safari. Boolean that indicates whether the child element need also be duplicated.
One of the following values:
|
Return value:
Returns the newly created node.
Example HTML code 1:
This example illustrates the use of the cloneNode method:
|
||||
<head> <script type="text/javascript"> function Clone () { var srcTable = document.getElementById ("srcTable"); var clonedTable = srcTable.cloneNode (true); clonedTable.id = ""; // clear the id property of the cloned table var container = document.getElementById ("container"); container.appendChild (clonedTable); } </script> </head> <body> <div id="container"> <table id="srcTable" border="1px"> <tr> <td>Apple</td> <td>Peach</td> <td>Cherry</td> </tr> </table> </div> <br /><br /> <button onclick="Clone ();">Clone Table!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
attribute, CommentNode, doctype, document, DocumentFragment, TextNode, XMLDocument
HTML elements:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blink, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, style, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
Related pages:
External links:
User Contributed Comments