×

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 Search Box

The HTML search box is also known as the search bar. It is an input text field that is used to search the content. If the users want to explore...

7 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.

How to insert spaces/tabs in text using HTML/CSS

What is HTML? HTML or HyperText Markup Language is used to design web pages. It was released in 1993. The file extension name for HTML file is “.html”. An HTML file...

4 minutes read.

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 &lt;center> Tag

The term HTML refers to Hyper Text Markup Language. The most widely used markup language for making web pages is HTML. A structure of web page is described in HTML....

3 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 Video

HTML VIDEO: HTML5 has introduced the <video> tag to embed video into the webpage directly without using the plug-ins.   Syntax: <video attributes >     <source src="address" type="video/mp4">     Your browser does not support the video tag.   </video> <video width="320" height="240" controls>     <source src="mountain.mp4" type="video/mp4">     Your browser does not support the video tag.   </video> Output: ← Prev ...

1 minute read.

HTML5 Formatting

HTML Formatting HTML Formatting is a method of text formatting for a better appearance and look. HTML allows us to format text without using the CSS. HTML includes a lot of formatting tags....

6 minutes read.

HTML Button Disabled

The HTML <button> element has many attributes, one of them is the disabled attribute. The button on which you apply the disabled attribute will not work, or we can say...

4 minutes read.

HTML Font Color

The HTML <font> tag has an attribute called color, which defines the color of the text. We use the color attribute inside the <font> tag to specify the color of...

6 minutes read.

Html &lt;dd> Tag

In an HTML description list, an item's definition or description is indicated using the definition description ("dd" tag). A <dd> tag can contain paragraphs, line breaks, images, links, and lists. It...

4 minutes read.

HTML <svg> tag

SVG is used to determine the vector-based graphics in XML format. What is SVG? We utilize SVG tag to specify visuals for the website. The attribute of SVG in a HTML document...

2 minutes read.

HTML References

BASIC HTML TAGS TAGS DESCRIPTION <!DOCTYPE> Defines Document Type <html> Defines Html document <head> Defines Header of document <title> Defines Title of document <body> Describe all the content of document <h1> to <h6> Defines Html Heading <p> Write Paragraph in this tag <br> Line break <!--...--> Comment in Html FORMATTING TAGS TAGS DESCRIPTION <abbr> Defines...

3 minutes read.

Html &lt;Col> Tag

You can write the col tag inside the table tag after the thead, tbody, foot, and tr tags. You can also apply it after the caption tag (in which the...

4 minutes read.

HTML Charsets

In this tutorial, we are going to discuss about the charsets in the HTML. The letters, numbers, and several other symbols are shown correctly by the web browser. All of this...

2 minutes read.

How to change text color in HTML

Changing the text colour of a web page is essential because it enhances readability. There are several ways to change the colour of the text in HTML, which are as follows: Text...

7 minutes read.

HTML <samp> tag

The Samp tag in HTML is used for defining the sample output from the computer program. This tag is a phrase type tag and a container tag that means the...

2 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 Text Format

Introduction HTML may format text in two different ways. The HTML tags define a text property to make it appear one way or another. A text element may also be formatted...

7 minutes read.

HTML <video> Tag

The Video tag in HTML is used to play video on a media player on the web page. We can also insert an audio by the video tag, but audio...

6 minutes read.