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
<!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> |
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
<!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> |
Output:
In console, you can see textContent of parent element.
Supported Browsers:
- Google Chrome
- Edge
- Firefox
- Apple Safari
- Opera
Recommended Posts:
- HTML | DOM HTML Object
- Replace a DOM element with another DOM element in place
- HTML | DOM isEqualNode() Method
- HTML | DOM History go() Method
- HTML | DOM Input Date stepUp() Method
- HTML | DOM console.warn() Method
- HTML | DOM console.clear() Method
- HTML | DOM blur() Method
- HTML | DOM createElement() Method
- HTML DOM queueMicrotask() Method
- HTML | DOM Location replace() Method
- HTML | DOM close() Method
- HTML | DOM createAttribute() Method
- HTML | DOM writeln() Method
- HTML | DOM console.trace() Method
- HTML | DOM createComment() Method
- HTML | DOM console.table() Method
- HTML | DOM console.time() Method
- HTML | DOM createTextNode() Method
- HTML | DOM console.error() Method
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.


