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
let text = document.createTextNode(data);
Example
Let's make a new text node in a document:
<div id="parent">
<div class="elem">
<p>text1</p>
<p>text2</p>
</div>
</div>
let parent = document.getElementById('parent');
let elem = parent.querySelector('.elem');
let text = document.createTextNode('text3');
parent.appendChild(text);
The code execution result:
<div id="parent">
<div class="elem">
<p>text1</p>
<p>text2</p>
<p>text3</p>
</div>
</div>
See also
-
the
createElementmethod
that can be used to create a new element