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

HTML DOM appendChild() Method

Last Updated : 03 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The HTML DOM appendChild() Method of the node interface, is used to create a text node as the last child of the node. This method is also used to move an element from one element to another element. It is used for creating a new element with some text then first create the text as the text node and then append it to the element, then append the element to the document.

Syntax:

node.appendChild(node);

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

We will use the document.createElement() method that is used to create the HTML element ie., an element specified with elementName will be created or an unknown HTML element will be created if the specified elementName is not recognized. We will also use the createTextNode() method to create the TextNode that will contain the element node and a text node. This method is used to provide text to an element that will contain the text values as parameters & it is of string type. The getElementById() method will return the elements containing the given ID which will be passed to the function. 

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




<!DOCTYPE html>
<html>
 
<head>
    <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>
     
    <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>
</body>
 
</html>


Output:

DOM appendChild() Method

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

HTML




<!DOCTYPE html>
<html>
 
<head>
    <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>
    <button onclick="geeks()">
        Submit
    </button>
    <script>
        function geeks() {
            var node = document.createElement("P");
            var t = document.createTextNode("GeeksforGeeks");
            node.appendChild(t);
            document.getElementById("sudo").appendChild(node);
        }
    </script>
</body>
 
</html>


Output:

DOM appendChild() Method

We have a complete list of HTML DOM methods, to check those please go through this HTML DOM Object Complete reference article.

Supported Browsers: The browser supported by DOM appendChild() Method are listed below:

  • Google Chrome 1.0 and above
  • Microsoft Edge 12.0 and above
  • Firefox 1.0 and above
  • Opera 7.0 and above
  • Safari 1.1 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Previous Article
Next Article

Similar Reads

PHP | DOMNode appendChild() function
The DOMNode::appendChild() function is an inbuilt function in PHP which is used to appends a child to an existing list of children or creates a new list of children. The child can be created with DOMDocument::createElement(), DOMDocument::createTextNode() or by using any other node. Syntax: DOMNode DOMNode::appendChild( DOMNode $newnode ) Parameter
2 min read
What is appendChild in JavaScript ?
In JavaScript, the appendChild() method is used to append a new child node to the end of the list of children of the specified parent node. This method is commonly used in DOM manipulation to add new elements to an existing HTML structure dynamically. Syntax:selectedElement.appendChild(newChild);Parameters:newChild: The new child node that will be
2 min read
HTML | DOM HTML Object
The HTML Object property in HTML DOM is used to represent or access the HTML &lt;html&gt; element with in the object. The &lt;html&gt; element is used to return the HTML document as an Element Object. Syntax: It is used to access a &lt;html&gt; element.var x = document.getElementsByTagName("HTML")[0]; It can also be used to access a &lt;html&gt; el
3 min read
Replace a DOM element with another DOM element in place
The task is to replace an element by another element in place using JavaScript. Below are the few methods: parentNode Property: This property returns the parent node of the defined node, as a Node object. Syntax: node.parentNode Return value: It returns a node object, which represents the parent node of a node, or null if there is no parent. replac
3 min read
Difference between Virtual DOM and Real DOM
DOM stands for Document Object Model it is the structural representation of the HTML Document. Real DOM is the actual structure represented in the User Interface while Virtual DOM is the memory representation of the same. It is a tree-like Structure consisting of all nodes in an HTML document DOM represents the Ul of your applications. Real DOMReal
3 min read
HTML DOM isEqualNode() Method
The HTML DOM isEqualNode() method checks whether the two nodes are equal or not. These nodes are considered equal if they are of the same type, having the same characteristics and the same attributes. The attributes do not have to be in the same order. Syntax: node.isEqualNode(othernode) Parameters: The "othernode" parameter is required in this fun
2 min read
HTML DOM History go() Method
The History go() method in HTML is used for loading a specific URL from the history list. It is a better alternative to history.back() and history.forward() methods if a number or the URL of the specific page is known and wants to load from your history. Syntax: history.go( number|URL )Parameters: This method accepts a single parameter number|URL w
2 min read
HTML | DOM Input Date stepUp() Method
The Input Date stepUp() method in HTML DOM is used to increment the value of date field by a specified number. The Input Date stepUp() method can only be used on the days and not on months and years. Syntax: inputdateObject.stepUp( number ) Parameters: This method accepts single parameter number which is used to specify the amount of days the date
2 min read
HTML DOM console warn() Method
The console.warn() method is used to write a warning message in the console. So open the console to display the output (warning message). Syntax: console.warn( message )Parameters: This method accepts single parameter message which is mandatory. This parameter is used to hold the warning message. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html
1 min read
HTML DOM console clear() Method
The console.clear() method in HTML is used to clear the console and writes some message "Console was cleared" on the console whenever it is executed. This method does not require any parameter. Syntax: console.clear()Example: The below program illustrates the console.clear() method in HTML: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt;
1 min read
Article Tags :
three90RightbarBannerImg