JS cloneNode

The cloneNode() method creates a copy of a node, and returns the clone.

The cloneNode() method clones all attributes and their values.

Syntax

node.cloneNode(deep)

Parameters

Parameter Description
deep Optional.
false - Default. Clone only the node and its attributes.
true - Clone the node, its attributes, and its descendants.

Return Value

Type Description
Node The cloned node.

html file

<!DOCTYPE html>
<html>
<head>
    <title>DOM Navigation</title>
    <style>
        #test{
            background: #ffff00;
            width: 800px;
            height: 200px;
            padding: 10px 10px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <ul id= "list1">
        <li class="abc">orange</li>
        <li>Apple</li>
        <li>Grapes</li> 
        <li>Banana</li>
    </ul>
    <ul id= "list2">
        <li>Carrot</li>
        <li>Reddish</li>
    </ul>
    <div id="test"></div>
    <script src="js/dom-create.js"></script>
</body>
</html>

dom-create.js

/* JavaScript CloneNode*/

var target = document.getElementById("list1").children[0];

var copyElement = target.cloneNode(true);

console.log(copyElement);


document.getElementById("list2").appendChild(copyElement);

document.getElementById("test").appendChild(copyElement);