English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
document.createTextNode(text)
var newtext = document.createTextNode(text); var p1 = document.getElementById("para"); p1.appendChild(newtext);Test and see‹/›
All browsers fully support the createTextNode() method:
Method | |||||
createTextNode() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
text | String containing the text to be placed in the 'text' node |
Return Value: | Text node object with created text node |
---|---|
DOM Version: | DOM Level1 |
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‹/›