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 »
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 »
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 consain attribute nodes.
Definition and Usage
The createTextNode()
method creates a text node.
Syntax
document.createTextNode(text)
Parameters
Parameter | Description |
text | Required. 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 | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |