You are here: Reference > JavaScript > client-side > HTML DOM > methods > createTextNode (document, XMLDocument)

createTextNode method (document, XMLDocument)

Browser support:
Creates a new TextNode from the specified text.
This method was introduced because TextNode elements cannot be created by the createElement method.
When the new CommentNode element is created, use the insertBefore or appendChild method to insert it into the DOM hierarchy.


object.createTextNode (data);
You can find the related objects in the Supported by objects section below.


Required. String that specifies the text content of the TextNode element. This content can be retrieved with the data property.

Return value:

Returns the newly created TextNode object.

Example HTML code 1:

This example illustrates the use of the createTextNode method:
    <script type="text/javascript">
        function CreateText () {
            var textContainer = document.getElementById ("textContainer");
            var textNode = document.createTextNode ("Dynamically generated text.");
            textContainer.appendChild (textNode);
    <div id="textContainer"></div>
    <button onclick="CreateText ();">Create a text node!</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content