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.


Description

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
NodeThe 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


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.