EN
JavaScript - get element child nodes
0 points
In this article, we would like to show you how to get element child nodes using JavaScript.
Quick solution:
xxxxxxxxxx
1
const container = document.querySelector('#container');
2
3
const nodes = container.childNodes;
4
5
for (let i = 0; i < nodes.length; i++) {
6
console.log(nodes[i]); // do something with nodes
7
}
In this example, we use childNodes
property to get all child nodes of the container element and log them in the console.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div id="container">Text node<span></span><div></div></div>
5
<script>
6
7
const container = document.querySelector('#container');
8
const nodes = container.childNodes;
9
10
for (let i = 0; i < nodes.length; i++) {
11
console.log(nodes[i]);
12
}
13
14
</script>
15
</body>
16
</html>