EN
JavaScript - appendChild() method example
0 points
In this article, we would like to show you appendChild()
method example in JavaScript.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<script>
5
6
// create element
7
var element = document.createElement('div');
8
9
// set inner text
10
element.innerText = "Child element's internal text...";
11
12
// append element to body
13
document.body.appendChild(element);
14
15
</script>
16
</body>
17
</html>
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div class="parent"></div>
5
<script>
6
7
var parentElement = document.querySelector('.parent');
8
9
// create child element
10
var childElement = document.createElement('div');
11
12
// set childElement innerText
13
childElement.innerText = "Child element's internal text...";
14
15
// append childElement to parentElement
16
parentElement.appendChild(childElement);
17
18
</script>
19
</body>
20
</html>