The Wayback Machine - https://web.archive.org/web/20250215181927/https://www.geeksforgeeks.org/html-dom-createelement-method/
Open In App

HTML DOM createElement() Method

Last Updated : 02 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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. Input : 

HTML
<!DOCTYPE html>
<html>

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

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

</html>

Output: 

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. Input : 

HTML
<!DOCTYPE html>
<html>

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

<body>
    <div id="divid"> A div element</div>
    <button onclick="createparagraph()">CreateParagraph</button>
</body>

</html>

Output: 

Supported Browser: The browsers supported by DOM createElement() Method are listed below:

HTML DOM createElement() Method – FAQs

Does createElement() immediately add the element to the document?

No, createElement() only creates the element in memory. You need to explicitly add it to the document using methods like appendChild(), prepend(), or insertBefore().

Can createElement() be used with SVG or XML namespaces?

For SVG or XML elements, it is better to use createElementNS() with the appropriate namespace to ensure correct creation of the element.

Can createElement() create custom elements or components?

Yes, createElement() can be used to create custom elements, including those defined by the Custom Elements API (like <my-custom-element>), provided the tag name follows the correct syntax.

Does createElement() sanitize input or prevent XSS attacks?

No, createElement() does not sanitize input or protect against XSS attacks. Always sanitize or validate input before setting properties like innerHTML to prevent security vulnerabilities.

How does createElement() handle invalid or unknown tag names?

If createElement() is called with an invalid or unknown tag name, it will still create an element, but browsers may treat it as an unknown or generic element. For HTML5, custom tags are often handled gracefully if they follow naming conventions.



Next Article

Similar Reads

three90RightbarBannerImg