Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is content within an element its child node?

Tags:

html

dom

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?

like image 605
Gwater17 Avatar asked Nov 01 '25 04:11

Gwater17


1 Answers

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
<div>Hello</div>
like image 194
Oriol Avatar answered Nov 02 '25 19:11

Oriol