Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Get All Child Nodes</title> </head> <body> <div id="main"> <h1 id="title">My Heading</h1> <p id="hint"><span>This is some text.</span></p> </div> <script> var main = document.getElementById("main"); // First check that the element has child nodes if(main.hasChildNodes()) { var nodes = main.childNodes; // Loop through node list and display node name for(var i = 0; i < nodes.length; i++) { console.log(nodes[i].nodeName); } } </script> <p><strong>Note:</strong> Please check out the browser console by pressing the f12 key on the keyboard.</p> </body> </html>