English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM createTextNode() Method

HTML DOM Document Object

createTextNode()method to create a text node with specified text.

UsecreateElement()method to create an element node with a specified name.

After creating the text node, useelement.appendChild()orelement.insertBefore()Method to insert it into an element.

Syntax:

document.createTextNode(text)
var newtext = document.createTextNode(text);
var p1 = document.getElementById("para");
p1.appendChild(newtext);
Test and see‹/›

Browser Compatibility

All browsers fully support the createTextNode() method:

Method
createTextNode()YesYesYesYesYes

Parameter Value

ParameterDescription
textString containing the text to be placed in the 'text' node

Technical Details

Return Value:Text node object with created text node
DOM Version:DOM Level1

More Examples

Create a <p> element with some text and append it to a <div> element:

var para = document.createElement("p");
var newContent = document.createTextNode("Hi there and greetings!");
para.appendChild(newContent);
document.getElementById("myDiv").appendChild(para);
Test and see‹/›

HTML DOM Document Object