The data property

The data property allows you to read the contents of an element's text node, including commented out text.

Syntax

javascriptelement.data;

Example

Let's output the text of the first child element using the data property:

html<body>
  text1 <!--com1-->
  text2 <!--com2-->
</body>
javascriptlet elem = document.body.firstChild;
console.log(elem.data); 

The code execution result:

javascript'text1'

Example

And now let's display the comment of the first child element:

html<body>
  text1 <!--com1-->
  text2 <!--com2-->
</body>
javascriptlet elem = document.body.firstChild;
let comment = elem.nextSibling;
console.log(comment.data);

The code execution result:

javascript'com1'

See also

  • the nodeValue method
    that sets a node value