iterate over node object children code example
Example: iterate over node object children
/* HTML */
/*
-
Item
-
Second item
-
Third item
*/
const secondaryNavList = document.querySelector('.navigation__secondary');
/* Method 1 A (ES6) */
const secondaryNavListChildren = Array.from(secondaryNavList.children);
for (let secondaryNavListItem of secondaryNavListChildren) {
console.log(secondaryNavListItem);
}
/* Method 1 B (ES5) */
const secondaryNavListChildren = Array.prototype.slice.call(secondaryNavList.children);
for (let secondaryNavListItem of secondaryNavListChildren) {
console.log(secondaryNavListItem);
}
/* Method 2 */
const secondaryNavListChildren = secondaryNavList.childNodes;
secondaryNavListChildren.forEach((navListItem) => {
console.log(navListItem);
})