
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM appendChild() Method
The HTML DOM appendChild() method is used to create and add a text node at the end of the list of child nodes. The appendChild() method can also be used to move an element from current position to a new position. Using appendChild() you can add new values to a list and can even add a new paragraph under another paragraph.
Syntax
Following is the syntax for appendChild() Method −
node.appendChild( node )
Here, the parameter node is the object that you want to append. It is a compulsory parameter value.
Example
Let us see an example of appendChild() Method −
<!DOCTYPE html> <html> <body> <p>Click the button to create a paragraph and append it to the div</p> <div id="SampleDIV"> A DIV element </div> <button onclick="AppendP()">Append</button> <script> var x=1; function AppendP() { var paragraph = document.createElement("P"); paragraph.innerHTML = "This is paragraph "+x; document.getElementById("SampleDIV").appendChild(paragraph); x++; } </script> </body> </html>
Output
This will produce the following output −
After clicking append 3 times: −
In the above example −
We have created a div with id “SampleDIV”. The appended node will act as child of this div.
<div id="SampleDIV"> A DIV element </div>
We have then a button named “Append” which will execute the function AppendP()
<button onclick="AppendP()">Append</button>
The AppendP() function first creates a paragraph (p) element and assigns it to variable paragraph. Then some text is added to paragraph using innerHTML and a variable x is appended to text. This variable is incremented each time we click the ”Append” button. At last we append the newly created paragraph as a child node of the div element −
var x=1; function AppendP() { var paragraph = document.createElement("P"); paragraph.innerHTML = "This is paragraph "+x; document.getElementById("SampleDIV").appendChild(paragraph); x++; }
- Related Articles
- HTML DOM addEventListener() Method
- HTML DOM normalize( ) Method
- HTML DOM write() Method
- HTML DOM writeln() Method
- HTML DOM insertAdjacentElement( ) Method
- HTML DOM insertAdjacentHTML( ) Method
- HTML DOM insertAdjacentText( ) Method
- HTML DOM insertBefore( ) Method
- HTML DOM isDefaultNamespace( ) Method
- HTML DOM isEqualNode( ) Method
- HTML DOM isSameNode( ) Method
- HTML DOM item( ) Method
- HTML DOM blur() Method
- HTML DOM click() method
- HTML DOM cloneNode() method
