EN
JavaScript - append multiple HTML elements at once
0 points
In this article, we would like to show you how to append multiple HTML elements at once using JavaScript.
Quick solution:
xxxxxxxxxx
1
element.append(chlidElement1, childElement2, childElementN);
Warning:
append()
method appeard in the major web browsers around 2016-2018.
In this example, we create new child elements and use append()
method to append them at once to the container
element.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div id="container"></div>
5
<script>
6
7
var container = document.querySelector('#container');
8
9
// create children elements
10
var newChild1 = document.createElement('div');
11
var newChild2 = document.createElement('div');
12
13
// set children text
14
newChild1.textContent = 'new child 1';
15
newChild2.textContent = 'new child 2';
16
17
// append children to the container
18
container.append(newChild1, newChild2);
19
20
</script>
21
</body>
22
</html>