Create a text node:
The result will be:
HTML elements often consists of both an element node and a text node.
To create a header (e.g. <h1>), you must create both an <h1> element and a text node:
Create a <h1> element with some text:
The result will be:
More "Try it Yourself" examples below.
The createTextNode() method creates a Text Node with the specified text.
Tip: Use the createElement() method to create an Element Node with the specified name.
Tip: After the Text Node is created, use the element.appendChild() or element.insertBefore() method to append it to an element.
Method | |||||
---|---|---|---|---|---|
createTextNode() | Yes | Yes | Yes | Yes | Yes |
Parameter | Type | Description |
---|---|---|
text | String | Required. The text of the Text node |
DOM Version: | Core Level 1 Document Object |
---|---|
Return Value: | A Text Node object with the created Text Node |
Create a <p> element with some text: