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 >neveropen</ 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 >neveropen</ h1 > < script > var parent = document.createElement("div"); parent.innerHTML = "neveropen : "; 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