I have some simple HTML and JS code I set up to get a better handle on traversing the DOM.
Here's my HTML:
<!DOCTYPE html>
<html>
<head>
<title>Sandbox</title>
</head>
<body>
<div>
<h1 id="title">Sandbox</h1>
<button id="clickMe" onclick="playingAroundWithNodes()">Play with nodes!</button>
</div>
</body>
<script type="text/javascript" src="Sandbox3.js"></script>
</html>
And here's my JavaScript:
function playingAroundWithNodes() {
//Getting a reference to some nodes!
var theHtmlNode = document.childNodes[1];
var theHeadNode = theHtmlNode.childNodes[0];
var theBodyNode = theHtmlNode.childNodes[1];
//Let's check out those nodes!
console.log("theHtmlNode is a " + theHtmlNode.nodeName + " type node.");
console.log("theHeadNode is a " + theHeadNode.nodeName + " type node.");
console.log("theBodyNode is a " + theBodyNode.nodeName + " type node.");
}
Here's the console log I get though:
theHtmlNode is a HTML type node.
theHeadNode is a HEAD type node.
theBodyNode is a #text type node.
What gives? Where the heck is that text node, that's not the title node is it? I'm confused and have played around with it a bunch (and found that the body node is in fact the 3rd child of HEAD according to js, but looking at the HTML that doesn't make sense to me). I could see it being a 3rd descendant or something, but I assumed child meant direct child... Any help appreciated!