Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <title>Accessing Child Nodes</title> </head> <body> <div id="blog-post"> <h2>Blog Post Title</h2> <p>Some interesting content...</p> <div class="comments"> <p>Comment 1</p> <p>Comment 2</p> <p>Comment 3</p> </div> </div> <script> const commentsContainer = document.querySelector('.comments'); const comments = commentsContainer.children; // Only includes element nodes // Display the number of comments document.body.insertAdjacentHTML('beforeend', `<h4>Number of comments: ${comments.length}</h4>`); </script> </body> </html>