for each child of element

Solutions on MaxInterview for for each child of element by the best coders in the world

showing results for - "for each child of element"
Rodrigo
04 Aug 2019
1/* HTML */
2/*
3<ul class="navigation__secondary" id="navigation-bar">
4  <li class="navigation__secondary-item">
5  	<span>Item</span>
6  </li>
7  <li class="navigation__secondary-item active">
8	  <span>Second item</span>
9  </li>
10  <li class="navigation__secondary-item active">
11  	<span>Third item</span>
12  </li>
13</ul>
14*/
15
16const secondaryNavList = document.querySelector('.navigation__secondary');
17
18/* Method 1 A (ES6) */
19const secondaryNavListChildren = Array.from(secondaryNavList.children);
20
21for (let secondaryNavListItem of secondaryNavListChildren) {
22  console.log(secondaryNavListItem);
23}
24
25/* Method 1 B (ES5) */
26const secondaryNavListChildren = Array.prototype.slice.call(secondaryNavList.children);
27
28for (let secondaryNavListItem of secondaryNavListChildren) {
29  console.log(secondaryNavListItem);
30}
31
32/* Method 2 */
33
34const secondaryNavListChildren = secondaryNavList.childNodes;
35
36secondaryNavListChildren.forEach((navListItem) => {
37  console.log(navListItem);
38})
39