MJGwater MJGwater - 1 year ago 58
HTML Question

is content within an element its child node?

enter image description here

When watching the tutorial the teacher said that html was a child node of DOCUMENT, but said the text "jQuery Adventures" was "under" the title node.

So is the text within an element is technically a child node of that element or does it just appear under it in the DOM? Does it ever matter when coding?

Answer Source

Yes, a contiguous run of text inside an element becomes a text node which is a child of that element.

var el = document.querySelector('div');
console.log(el.childNodes.length); // 1 child node
console.log(el.childNodes[0].nodeType === Node.TEXT_NODE); // text node
console.log(el.childNodes[0].nodeValue); // text contents