HTML DOM ParentNode.append() Method

The ParentNode.append() method is used to insert Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as Text nodes.

Syntax:

ParentNode.append( ChildNodesToPrepend );

Parameters: This parameter holds the set of Node or DOMString objects that is to be inserted as the last child of parent element.

Below examples illustrate the ParentNode.append() method:

Example 1: In this example, we will append an element. To show this method, we have created three elements parentNode, Child1 and Child2. Then we have appended the Child1 and Child2 to parentNode.



In console, we had shown childNodes of parentNode.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
  
    <script>
        var parentNode = document.createElement("div");
        var Child1 = document.createElement("p");
        var Child2 = document.createElement("div");
        parentNode.append(Child1);
        parentNode.append(Child2);
        console.log(parentNode.childNodes); 
    </script>
</body>
  
</html>

chevron_right


Output:

In console, you can see childNodeList of parentNode. One is p and one is div.

Example 2: In this example, we have appended some text to innerHTML of the element and the element’s textContent.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<body>
    <h1>GeeksforGeeks</h1>
  
    <script>
        var parent = document.createElement("div");
        parent.innerHTML = "GeeksforGeeks : ";
        parent.append("A Computer Science Portal for Geeks");
        console.log(parent.textContent); 
    </script>
</body>
  
</html>

chevron_right


Output:

In console, you can see textContent of parent element.

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Apple Safari
  • Opera

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.