The idea was to console log node value. But instead of names it returns null. I don't see why, because code seems fine to me. So, I want understand what happening. I found how to make it work, but I don't understand why my code don't work. Code and results:
HTML
<div>Users:</div>
<ul id="myList">
<li>John</li>
<li>Doe</li>
</ul>
JavaScript
let listNode = document.body.children[1].children[1]
console.log(listNode)
// Why not return node value?
let value = listNode.nodeValue
console.log(value)
Results: link
When representing HTML elements (DOM Objects) in JavaScript, everything is a node - - even the text within an element. But, not all nodes are elements. So when you got a reference to an
<li>, that<li>wasn't the node that contained the name, it was the child text node of that<li>. Another way to say this is that element nodes don't ever have a value of their own, their children do and that is why you were gettingnullwhen you tried to get thenodeValueof an<li>To get that content, you must navigate all the way down to that node:
But, the DOM also exposes other ways to go straight to the content within an element via
.textContentand.innerHTML: