HTML DOM appendChild() Method
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.
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.
Example 2: This example describes the use of the appendChild() method by appending the text to the document after clicking the submit button.
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
- Internet Explorer 5.0 and above
- Microsoft Edge 12.0 and above
- Firefox 1.0 and above
- Opera 7.0 and above
- Safari 1.1 and above