The data property
The data property allows
you to read the contents of an
element's text node, including
commented out text.
Syntax
element.data;
Example
Let's output the text of the first child
element using the data property:
<body>
text1 <!--com1-->
text2 <!--com2-->
</body>
let elem = document.body.firstChild;
console.log(elem.data);
The code execution result:
'text1'
Example
And now let's display the comment of the first child element:
<body>
text1 <!--com1-->
text2 <!--com2-->
</body>
let elem = document.body.firstChild;
let comment = elem.nextSibling;
console.log(comment.data);
The code execution result:
'com1'
See also
-
the
nodeValuemethod
that sets a node value