HTML Academy
Cloning and inserting elements, part 3
Working with DOM17/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”
How to clone elements
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Cloning and inserting elements, part 4

We already covered how to clone elements, but we have not yet tried inserting several copies on a page.

Let’s start a loop, and in the body of this loop clone a template, change the contents of the child element, and subsequently insert copies on the page.

This is similar to using a stencil for drawing. We apply the stencil many times, and it guides our drawing as we need it. We work with a single stencil throughout the process.

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> <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 and fill elements in the loop // for (var i = 1; i <= 10; i++) { // var clonedElement = element.cloneNode(true); // clonedElement.children[0].textContent = i; // }

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 block with the loop.
    2. Add clonedElement to the end of pool inside the loop, after all of the operations.

    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.