HTML Academy
Cloning and inserting elements, part 1
Working with DOM15/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 3
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Cloning and inserting elements, part 2

We inserted the template, and after that we inserted another element. And again we tried to insert the template. As a result, the template only appeared by itself at the end of the list. Why did this happen?

Because there is only one element, even if it is a template. We cannot insert one element several times in different places on the same page. After all, we can’t be in several places at the same time, can we? So DOM elements can’t either.

Therefore, that is why we clone DOM elements. We can clone any elements, including templates, and insert these copies on the page as many times as we like.

First, let’s see how cloning works. To do this, you can use the append method. It returns a cloned element.

Please note that this method has an argument that is a Boolean value. However, if you pass the value false, then the element itself with its classes and attributes will be cloned, but any child elements will be excluded.

element.cloneNode(false); // Returns the cloned element without any nested elements.

Try to clone an element, pass the argument false to the cloneNode, and insert this element on the page.

In the next assignment we will learn what happens if we pass the true argument or even no arguments at all to cloneNode. Spoiler: Everything is all quite strange.

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);

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. Uncomment the line with the cloned element.
    2. Output clonedElement to the console.
    3. Output 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.