HTML DOM createTextNode() Method

Last Updated : 10 Aug, 2026

The createTextNode() method is used to create a TextNode which contains an element node and a text node. It is used to provide text to an element. This method contains the text values as parameters which are of string type.

Syntax

document.createTextNode( text )

Parameters: This method accepts single parameter text which is mandatory. It is used to specify the text of the text node.

Example: In this example, we will use createTextNode() method

HTML
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
  
<head>
    <title>DOM createTextNode() Method</title>
    <style>
        h1,
        h2 {
            color: green;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <h1>GeeksForGeels</h1>
    <h2>DOM createTextNode() Method</h2>
<!--Driver Code Ends-->

    <button onclick="geeks()">Submit</button>
    <script>
        function geeks() {
            let x =document.createTextNode("GeeksForGeeks");
            document.body.appendChild(x);
        }
    </script>

<!--Driver Code Starts-->
</body>
  
</html>
<!--Driver Code Ends-->
  • Automatically escapes HTML characters : Unlike setting innerHTML, any <, >, &, etc. in the text are treated as plain text, preventing XSS and unintended HTML parsing.
  • Creates a pure Text node (nodeType 3) : The result is a lightweight text-only node with no element wrapper, which is ideal when you need precise control over text content without adding extra tags.
Comment