HTML Academy
Cloning and inserting elements, part 2
Working with DOM16/23
Back to the list of tasks
  • 1. What are you planning to do?
  • 2. The “change” event
  • 3. How the “change” event works
  • 4. Deleting the element from the list
  • 5. How to check the collection length
  • 6. Debugging the code
  • 7. Static and live collections
  • 8. Displaying the message on the page
  • 9. The “submit” event
  • 10. Cancelling the form submission
  • 11. How to obtain text from the input field
  • 12. Templates and the <template> tag
  • 13. The contents of the <template> tag, document-fragment
  • 14. Cloning and inserting elements, part 1
  • 15. Cloning and inserting elements, part 2
  • 16. Cloning and inserting elements, part 3
  • 17. Cloning and inserting elements, part 4
  • 18. How to clone elements
  • 19. Adding a new element to a list
  • 20. Deleting a new task in the list
  • 21. Clearing the input field
  • 22. Summary of “Actions with DOM”
  • 23. The third program: “Instant messenger”
Cloning and inserting elements, part 4
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Cloning and inserting elements, part 3

If the element is copied without its nested elements when false is passed to cloneNode, then the exact opposite is true when passing true. In this case, the element itself is cloned along with all all of the nested elements. This type of cloning includes all attributes, classes, and the text contents of all nested elements. We call this deep cloning.

Previously, the cloneNode method behaved like this by default, even when true was not passed. Under the new standard, this behavior has changed, and the method should perform non-deep copying by default without needing to be passed an argument (where before you would have had to pass false to it). However, the standard only proposes recommendations, whereas it is up the browser whether to follow them or not. Currently not all browsers have switched to the new standard, and in order to ensure backward compatibility they retain the old behavior by performing deep cloning by default.

Therefore, it is best to always pass a Boolean value to avoid unpredictable program behavior.

element.cloneNode(true);  // Returns a cloned element with all nested elements
element.cloneNode();      // 0_o

Let’s take a look at how deep cloning works in practice. Copy the template, add text content to its child element, and add the template to the page.

Comments

  • index.html
  • style.css
  • script.js
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Cloning elements</title> <link href="style.css" rel="stylesheet"> </head> <body> <div class="pool"> <div class="el"> <span>1</span> </div> <div class="el"> <span>2</span> </div> </div> <template id="element-template"> <div class="el"> <span><!--Index of the element--></span> </div> </template> <script src="script.js"></script> </body> </html>
CSS
.pool { display: flex; flex-wrap: wrap; min-height: 330px; margin: 20px; padding: 10px; background-color: #e9e9f0; } .el { display: flex; justify-content: center; align-items: center; width: 150px; height: 150px; margin-right: 10px; margin-bottom: 10px; box-sizing: border-box; border: 20px solid #3366ff; background-color: #99b2ff; color: rgba(255, 255, 255, 0.9); font: bold 60px "Arial", sans-serif; text-align: center; } .el--green { background-color: #a3d095; border: 20px solid #518d3f; }
JavaScript
// Cards container var pool = document.querySelector('.pool'); // Get the card template var template = document.querySelector('#element-template').content; var element = template.querySelector('div'); // Clone an element var clonedElement = element.cloneNode(false); // Add a number to span // clonedElement.children[0].textContent = 3; console.log(clonedElement);

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Click inside the mini browser to put the focus in this window.

100%
Console
Goalscompleted
0
    1. On the line with the cloned element, change false to true, and take a look at the console output.
    2. Uncomment the line with the added number in the span.
    3. After the result is output to the console, add clonedElement to the end of the pool block.

    Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

    VISAMastercard

    Log in

    or

    Forgot your password?

    Sign up

    Sign up

    or
    Log in

    Restore access

    Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

    Forgot to connect your email to the profile? Email us and we’ll help.