EN
JavaScript - iterate over elements from HTMLCollection
0 points
In this article, we would like to show you the best way to iterate over elements from HTMLCollection working with JavaScript.
Quick solution:
xxxxxxxxxx
1
for (let element of elements) {
2
// ...
3
}
In this example, we use the for...of
statement to iterate over the children of the container
element and display their outerHTML in the console.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div id="container">
5
<div>Div-1 example text...</div>
6
<div>Div-2 example text...</div>
7
<div>Div-3 example text...</div>
8
</div>
9
<script>
10
11
let container = document.querySelector('#container');
12
let elements = container.children;
13
14
for (let element of elements) {
15
console.log(element.outerHTML);
16
}
17
18
</script>
19
</body>
20
</html>