createTextNode method

The createTextNode method creates a new text node. In the parameter, the method receives the data to be inserted into the new text node.

Syntax

javascriptlet text = document.createTextNode(data);

Example

Let's make a new text node in a document:

html<div id="parent">
  <p>text1</p>
  <p>text2</p>
</div>
javascriptlet parent = document.getElementById('parent');

let text = document.createTextNode('text3');
parent.appendChild(text);

The code execution result:

html<div id="parent">
  <p>text1</p>
  <p>text2</p>
  text3
</div>

See also

  • the createElement method
    that can be used to create a new element