Languages
[Edit]
EN

JavaScript - append multiple HTML elements at once

0 points
Created by:
Kenya-Spears
860

In this article, we would like to show you how to append multiple HTML elements at once using JavaScript.

Quick solution:

element.append(chlidElement1, childElement2, childElementN);

Warning: append() method appeard in the major web browsers around 2016-2018.

 

Practical example

In this example, we create new child elements and use append() method to append them at once to the container element.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <div id="container"></div>
  <script>

    var container = document.querySelector('#container');
    
    // create children elements
    var newChild1 = document.createElement('div');
    var newChild2 = document.createElement('div');

    // set children text
    newChild1.textContent = 'new child 1';
    newChild2.textContent = 'new child 2';
    
    // append children to the container
    container.append(newChild1, newChild2);

  </script>
</body>
</html>

References

  1. Element.append() - Web APIs | MDN

Alternative titles

  1. JavaScript - append multiple child elements at once
  2. JavaScript - append multiple children elements at once
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join