HTML DOM Document createTextNode() method

Definition and usage

createTextNode() Methods create text nodes.

See also:

Document createElement() method

Element appendChild() method

Element insertBefore() method

HTML elements are nodes

All HTML elements are nodes.

Elements are nodes. Attributes are nodes. Text is nodes.

Some elements contain other nodes.

Some elements contain text nodes.

Some elements contain attribute nodes.

Example

Example 1

Create a text node and append it to the document:

let textNode = document.createTextNode("Hello World"); document.body.appendChild(textNode);

Try it yourself

Example 2

Create an <h1> element with a text node:

const h1 = document.createElement("h1");
const textNode = document.createTextNode("Hello World");
h1.appendChild(textNode);

Try it yourself

Example 3

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

Syntax

document.createTextNode(data)

parameter

parameter description
data required. The text of the node.

return value

type description
node A newly created text node representing the specified data string.

Browser support

document.createTextNode() It is a DOM Level 1 (1998) feature.

It is supported by all browsers:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
supports 9-11 supports supports supports supports