78
Difference between childNodes and children properties
2021-05-12 - JavaScript
1// JS Tip of the Day
2// Difference between childNodes and children properties
3
4/*
5 ...
6 <article class="hero_card">
7 <h4>Batman</h4>
8 <p>Batman was created by artist Bob Kane and writer Bill Finger.</p>
9 <!-- This is a comment (node) -->
10 <ul>
11 <li>Batwing</li>
12 <li>Batwing</li>
13 </ul>
14 This is a text (node)
15 </article>
16 ...
17*/
18
19// childNodes returns a live NodeList of child nodes (elements, text and comments)
20console.log(document.querySelector('.hero_card').childNodes.length); // 9
21
22// children returns a live NodeList of child nodes (only element)
23console.log(document.querySelector('.hero_card').children.length); // 3
#JStipoftheday