There are two ways to append HTML code to a div through JavaScript
- Using the innerHTML attribute
- Using the insertAdjacentHTML() method
Using the innerHTML attribute:
To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.
Syntax:
element.innerHTML += "additional HTML code"
or
element.innerHTML = element.innerHTML + "additional HTML code"
Example:
<!DOCTYPE html> <html> <head> <title>How to Append HTML Code to a Div using Javascript</title> <style> body { text-align: center; padding: 5%; } h1{ color:green; } </style> </head> <body> <div id="add_to_me"> <h1>GeeksforGeeks</h1> <p>This is the text which has already been typed into the div</p> </div> <button onclick="addCode()">Add Stuff</button> <script> function addCode() { document.getElementById("add_to_me").innerHTML += "<h3>This is the text which has been inserted by JS</h3>"; } </script> </body> </html> |
- Output:
- Before Clicking the Button:

- After Clicking the Button:

- The position (in the document) where you want to insert the code (‘afterbegin’, ‘beforebegin’, ‘afterend’, ‘beforeend’)
- The HTML code you want to insert enclosed in quotes
Note: This method basically destroys all the content of the div and recreates it. So, if you have any listeners attached to the child nodes of that div, they will be lost.
Using the insertAdjacentHTML() method
HTML code can be appended to a div using the insertAdjacentHTML() method. However, you need to select an element inside the div to add the code. This method takes two parameters:
Syntax:
elementInsideDiv.insertAdjacentHTML('afterend', 'additional HTML code');
Example:
<!DOCTYPE html> <html> <head> <title>How to Append HTML Code to a Div using Javascript</title> <style> body { text-align: center; padding: 5%; } h1{ color: green } </style> </head> <body> <div id="add_to_me"> <h1>GeeksforGeeks</h1> <p id="add_after_me"> This is the text which has already been typed into the div</p> </div> <button onclick="addCode()">Add Stuff</button> <script> function addCode() { document.getElementById("add_after_me").insertAdjacentHTML("afterend", "<h3>This is the text which has been inserted by JS</h3>"); } </script> </body> </html> |
- Output:
- Before Clicking the Button:

- After Clicking the Button:

- How to append data to <div> element using JavaScript ?
- How to Combine multiple elements and append the result into a div using JavaScript ?
- How to overlay one div over another div using CSS
- How to copy the content of a div into another div using jQuery ?
- How to center a div within another div?
- How to clear all div's content inside a parent div ?
- How to rotate an HTML div element 90 degrees using JavaScript ?
- How to clear the content of a div using JavaScript?
- Print the content of a div element using JavaScript
- How to find the width of a div using vanilla JavaScript?
- How to add a tooltip to a div using JavaScript?
- How to get the <div> height using JavaScript ?
- How to hide div element by default and show it on click using JavaScript and Bootstrap ?
- How to Change Background Color of a Div on Mouse Move Over using JavaScript ?
- How to remove specific div element by using JavaScript?
- How to scroll to an element inside a div using javascript?
- How to take screenshot of a div using JavaScript ?
- How to create editable div using JavaScript ?
- How to create three boxes in the same div using HTML and CSS ?
- How to check if the clicked element is a div or not in JavaScript ?
Note:You should never insert the HTML code in this way if you are taking it as an input from the user. It opens your website to cross-site scripting vulnerabilities.
Recommended Posts:
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.
Improved By : JacobAntony1


