HTML DOM Document createTextNode()
Examples
Create a text node and append it to the document:
let textNode = document.createTextNode("Hello World");
document.body.appendChild(textNode);
Try it Yourself »
Create a <h1> element with a text node:
const h1 = document.createElement("h1");
const textNode = document.createTextNode("Hello World");
h1.appendChild(textNode);
Try it Yourself »
const textNode = document.createTextNode("Hello World");
h1.appendChild(textNode);
Create a <p> element with a text node:
const para = document.createElement("p");
const textNode = document.createTextNode("Hello World");
para.appendChild(textNode);
Try it Yourself »
const textNode = document.createTextNode("Hello World");
para.appendChild(textNode);
HTML Elements are Nodes
All HTML elements are nodes.
Elements are nodes. Attributes are nodes. Texts are nodes.
Some elements contain other nodes.
Some elements contain text nodes.
Some elements contain attribute nodes.
Description
The createTextNode()
method creates a text node.
Syntax
document.createTextNode(text)
Parameters
Parameter
Description
text
Required.
The text for the node.
The text for the node.
Return Value
Type
Description
Node The created text node.
Browser Support
document.createTextNode()
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |