HTML DOM createElement() Method

Last Updated : 10 Aug, 2026

In an HTML document, the document.createElement() is a method used to create the HTML element. The element specified using elementName is created or an unknown HTML element is created if the specified elementName is not recognized. 

Syntax

let element = document.createElement("elementName");

In the above syntax, elementName is passed as a parameter. elementName specifies the type of the created element. The nodeName of the created element is initialized to the elementName value. The document.createElement() returns the newly created element. 

Example 1: This example illustrates how to create a <p> element.

HTML
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>

<head>
<!--Driver Code Ends-->

    <script>
        function createparagraph() {
            let x = document.createElement("p");
            let t =
                document.createTextNode("Paragraph is created.");
            x.appendChild(t);
            document.body.appendChild(x);
        }
    </script>

<!--Driver Code Starts-->
</head>

<body>
    <button onclick="createparagraph()">CreateParagraph</button>
</body>

</html>
<!--Driver Code Ends-->

Explanation:

  • Start with creating an <p> element using document.createElement().
  • Create a text node using document.createTextNode().
  • Now, append the text to <p> using appendChild().
  • Append the <p> to <body> using appendChild().

Example 2: This example illustrates how to create a <p> element and append it to a <div> element.

HTML
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>

<head>
<!--Driver Code Ends-->

    <script>
        function createparagraph() {
            let x = document.createElement("p");
            let t =
                document.createTextNode("Paragraph is created.");
            x.appendChild(t);
            document.getElementById("divid").appendChild(x);
        }
    </script>

<!--Driver Code Starts-->
</head>
<body>
    <div id="divid"> A div element</div>
    <button onclick="createparagraph()">CreateParagraph</button>
</body>

</html>
<!--Driver Code Ends-->
  • Automatically lowercases the tag name in HTML documents : When you call document.createElement("DIV") on an HTML page, the browser converts it to "div" before creating the element, matching HTML’s case-insensitive nature.
  • Supports Custom Elements via an options parameter : You can pass a second argument ({ is: "my-custom-button" }) to create a customized built-in element that extends a native HTML tag.
Comment