HTML DOM appendChild() Method

Last Updated : 22 Aug, 2026

The HTML DOM appendChild() method adds a new child node to the end of a specified parent node's child list. It can move existing nodes or add new nodes, allowing dynamic updates to the document structure by appending elements, text, or other nodes.

Syntax

node.appendChild(node);

Parameters: This method accepts a single parameter node which is mandatory and used to specify the node object that needs to be appended.

Example 1: This example describes the use of the appendChild() method that will create the text node as the last child of the node.

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

<head>
    <title>
        HTML DOM appendChild() Method
    </title>
    <style>
        h1,
        h2 {
            font-weight: bold;
            color: green;
        }
        body {
            margin-left: 130px;
        }
    </style>
</head>
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM appendChild() Method</h2>
    <ul id="gfg">
        <li>Computer Network</li>
        <li>Data Structures using C</li>
    </ul>
<!--Driver Code Ends-->

    <button onclick="geeks()">
        Submit
    </button>
    <script>
        function geeks() {
            let node = document.createElement("LI");
            let textnode = document.createTextNode("Web Technology");
            node.appendChild(textnode);
            document.getElementById("gfg").appendChild(node);
        }
    </script>

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

Example 2: This example describes the use of the appendChild() method by appending the text to the document after clicking the submit button.

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

<head>
    <title>
        HTML DOM appendChild() Method
    </title>
    <style>
        #sudo {
            border: 1px solid green;
            background-color: green;
            margin-bottom: 10px;
            color: white;
            font-weight: bold;
        }
        h1,
        h2 {
            text-align: center;
            color: green;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM appendChild() Method</h2>
    <div id="sudo">
        The Good Website is learning for Computer Science is-
    </div>
<!--Driver Code Ends-->

    <button onclick="geeks()">
        Submit
    </button>
    <script>
        function geeks() {
            let node = document.createElement("P");
            let t = document.createTextNode("GeeksforGeeks");
            node.appendChild(t);
            document.getElementById("sudo").appendChild(node);
        }
    </script>

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

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

Used Methods with appendChild()

Also read :

Comment