×

How to append HTML code to a div using JavaScript?

In HTML, we have a div tag which is used to create a box where we can put a lot of other tags. So it works like a container. So, to append any html code to the div tag, simply use the code and write between these tags. If we want to add any html code dynamically to a particular div tag, then we have to target or selectively choose that particular tag using either className or id name.

As we know, we can afford two or more divs with the same className, so using className will append the code to each div with that class name. So generally, we prefer to use an id name instead of a class name.

We can use two methods or attributes to target and append our HTML code:

  1. Using innerHTML attribute
  2. Using insertAdjacentElement() method to that element.

1.innerHTML attribute

Syntax:

targeted_element.innerHTML=targeted_element.innerHTML+new_HTMLcode

We can use shorthand properties of adding like:

targeted_element.innerHTM+=new_HTMLcode

For example:

<!DOCTYPE html>
<html>


<head>
	<title>How to Append HTML Code to a Div using JavaScript</title>
	<style>
		body {
			text-align: center;
			font-family:verdana;
			padding: 5%;
		}
		h1{
			color:green;
		}


		button{
			border-radius:5px;
			background-color:cyan;
			outline:none;
		}
	</style>
</head>


<body>
	<div id="first">
		<h1>JavatPoint</h1>
		<p>This is line is always visible to user</p>
	</div>


	<button onclick="doSomeThing()">Add Magic</button>
	<script>
		function doSomeThing() {
			document.getElementById("first").innerHTML +=
			"<h3>This line is presented after clicking the button</h3>";
		}
	</script>
</body>


</html>

Output:

How to append HTML code to a div using JavaScript

Explanation

In the above code, we have a file with HTML, CSS and JavaScript embedded in the same file. We have one div which has id ‘first’. We have one button, and when we click the button, it will run the doSome() method.

Now, if we see the definition of the doSome() method, then we will see we have targeted the div using its id and append the HTML code in string format using double quotes. If we don’t use the double quotes, then it will throw an error.

Whenever we click that button, it will add an h3 element to our tag, which will be displayed on the screen.

Basically, whenever we use the innerHTML attribute, it creates that element again and adds the new HTML code into it and displays it on screen.

2.insertAdjacentHTML() method

Here, we will use the insertAdjacentHTML() method to target the element, and we will put the location where we have to append.

We have four positions:

  1. 1.beforebegin
  2. 2.beforeend
  3. 3.afterbegin
  4. 4.afterend

For example

<!DOCTYPE html>
<html>


<head>
	<title>How to Append HTML Code to a Div using JavaScript</title>
	<style>
		body {
			text-align: center;
			font-family:verdana;
			padding: 5%;
		}
		h1{
			color:green;
		}


		button{
			border-radius:5px;
			background-color:cyan;
			outline:none;
		}
	</style>
</head>


<body>
	<div id="first">
		<div id ="second">
		    <h1 id="heading">JavatPoint</h1>
		  <p id="para">This is line is always visible to user</p>
		</div>
	</div>


	<button onclick="doSomeThing()">Add Magic</button>
	<script>
		function doSomeThing() {
			document.getElementById("second").insertAdjacentHTML('beforebegin',
			"<p>This line is presented after clicking the button and before the starting of second div</p>");


			document.getElementById("second").insertAdjacentHTML('beforeend',
			"<p>This line is presented after clicking the button and before the ending of second div</p>");


			document.getElementById("second").insertAdjacentHTML('afterbegin',
			"<p>This line is presented after clicking the button and just before the JavatPoint</p>");
		
			document.getElementById("second").insertAdjacentHTML('afterend',
			"<p>This line is presented after clicking the after ending the second div </p>");
		}
	</script>
</body>


</html>

Output:

How to append HTML code to a div using JavaScript

Explanation

  • In the above code, we have one div with id as ‘first’, and it has a child div named as ‘second '.
  • In the child div, we have one h1 tag and one p tag. We have one button which will run the function when it is clicked. We have used insertAdjacentHTML four times to try out different positions.
  • The first time we applied it on div second and position is ‘beforebegin’ so it will append the HTML code before the beginning of div named second.
  • In the second time, we applied it on the div second, but this time position is ‘beforeend’. So now it will append the HTML code just before ending the div ‘second’.
  • The third time we applied it on the div second, and now this time position is ‘afterbegin’, which means we can append the HTML code just after the starting of div ‘second’.
  • The last time we applied it on div second, and now the position is ‘afterend’ which means we will be able to append the HTML code just after the ending of div ‘second.’

Related Topics

HTML <script> Tag

For embedding a client -side script, we use script tag in html tag. This <script> tag can be used for containing scripting statements. It can also point another external script file...

2 minutes read.

HTML <sub> tag

The sub element in HTML is used to describe one or more subscript texts. If The sub tag shows the text which is presented in a smaller font and below...

2 minutes read.

HTML Reset Button

The HTML reset button is used to reset all values in the form. It clears all input fields. Reset is the value of the type attribute of the button element. The...

6 minutes read.

Html Wbr Tag

The Line Break Opportunity element A word break opportunity is a place in text from where the browser breaks a line. This opportunity is represented by the element wbr in the...

2 minutes read.

HTML Canvas

HTML Canvas: Using JavaScript, the < canvas > tag in HTML is used for drawing graphics on web page. It can be used to draw routes, boxes, text, gradient, and...

3 minutes read.

HTML Nested Table

HTML nested table refers to a table that is created inside another table. The table is in the form of rows and columns. From left to right are called horizontal rows,...

5 minutes read.

Difference between HTML and DHTML

Introduction to HTML HTML stands for Hypertext Markup Language. It is a standard web language used to create web pages and applications. HTML is used to define the structure and content...

4 minutes read.

HTML SVG

HTML SVG: SVG stands for Scalable Vector Graphics which defines the graphics for the web. It is a language for describing 2-D graphics in XML. Each drawn shape in SVG is...

1 minute read.

HTML <style> tag

The section of information that we want to style is contained in html style tag. It includes CSS, which is used to design the text and images in the web page. Syntax: <head> <title>…………....

3 minutes read.

HTML Hide Element

The HTML hide element is used to hide the element. We place the hidden attribute inside those elements which we want to hide. The hidden attribute is a Boolean attribute. The...

3 minutes read.

HTML Tutorial

Introduction This HTML tutorial helps beginners and professionals to learn HTML easily. HTML is a widely used web technology for website development. It stands for Hyper Text Markup Language. It is mainly used to...

7 minutes read.

How to append HTML code to a div using JavaScript?

In HTML, we have a div tag which is used to create a box where we can put a lot of other tags. So it works like a container. So,...

4 minutes read.

HTML <body> tag

HTML means Hyper Text Markup Language, which is most preferred for making Web pages. It explains how a Web page is put together. There are many different elements in HTML,...

3 minutes read.

HTML <table> tag

The Table element If we want to show the data and information in a two-dimensional table with rows and columns, then we can achieve this by using the HTML element <table>. Syntax: <body> <table> <thead> <tr> <thcolspan="#">………….</th> </tr> </thead> <tbody> <tr> <td>……….</td> <td>……….</td> </tr> </tbody> </table> </body> Example: <!DOCTYPE...

9 minutes read.

What is a Container tag?

In HTML, there are different types of tags or say instructions which make the developer specify various elements in the document and make the browsers understand what to display. Tags in...

5 minutes read.

HTML Date

The input element in HTML has several attributes. One of its attributes is the type attribute which creates a calendar in an order that a user can select a date...

5 minutes read.

HTML Code

Before jumping into the topic, let’s understand a common scenario where we have a code snippet in our HTML file. By code snippet, we mean computer code, which is written...

5 minutes read.

Responsive HTML5 and CSS3 Tutorial

Introduction to HTML5 Tutorial HTML5 is the latest and upgraded version of HTML. HTML is a hypertext markup language. Technically, it is not a programming language. It is a markup language. HTML...

17 minutes read.

CSS Clearfix

CSS Clearfix: A clearfix (or clear float) in CSS is for a component to clear or fix the child component, so we don’t need to insert additional markup. The clear...

4 minutes read.

Font tag in HTML

HTML stands for Hypertext Mark-up Language and is used to create web pages and other information that can be later displayed in a web browser. It also consists of a...

4 minutes read.