createTextNode (W3C DOM Core method)
| Version | Depr. | Static |
|---|---|---|
| DOM1 | No | No |
| IE5.5+ | FF1.5+ | SA1.3+ | OP9+ |
|---|---|---|---|
| Full | Full | Full | Full |
- Returns
Text
Example
var text = document.createTextNode('Mmm ... something');
The
example above creates a new Text node and saves it
to the variable text. The node can then be added to an
element like this:
element.appendChild(text);
The
argument to createTextNode can only be plain text —
you cannot pass entity references (for that you would need the createEntityReference method, which is poorly supported) —
however you can pass unicode characters, such as \u00a0
for a non-breaking space.
Since the createTextNode method returns the created Text node, you can also use it directly as the argument
to appendChild, which slightly reduces the
amount of code (and typing!) involved:
element.appendChild(document.createTextNode('Mmm ... something'));
Arguments
- data (
DOMString) required The data for the text node.
Description
Create a Text node of the specified string value.
The
created text node can then be added to the document using Node methods such as appendChild or insertBefore.
Return value
The created Text node.
Compatibility
| Internet Explorer | Firefox | Safari | Opera | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 5.5 | 6.0 | 7.0 | 1.5 | 2.0 | 3.0 | 1.3 | 2.0 | 3.0 | 9.0 | 9.5 |
| Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full |
No known issues.
User-contributed notes
There are no comments yet.
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.