Insert HTML Code anywhere With JavaScript

In this tutorial we will see how to Insert HTML Code anywhere With JavaScript. We have used insertAdjacentHTML Method for this, using which we can add HTML content before or after or even inside any HTML tag.

HTML Code

HTML Code is given below, This code contains two paragraph tags with dummy text in them. Each p tag has it's own unique id.

<!DOCTYPE html>
<html>
<head>
  <title>Insert HTML Code anywhere With JavaScript</title>
</head>
<body>
  <p id="one">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
  <p id="two">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>    
</body>
</html>

JavaScript Code

Take a look at the JavaScript code, this code is further explained below.

<script>
var ref =document.getElementById('two');
var htmlCode = '<div><h1>Heading</h1><p>Paragraph</p></div>';
ref.insertAdjacentHTML('afterend',htmlCode); 
</script>

Demo

getElementById() Method

getElementById() method is used to target or select the HTML element. In this example it is used to select the HTML element after which or before which, we can add HTML content.

insertAdjacentHTML Method

The insertAdjacentHTML inserts HTML Content at a specified position, The predefined positions that we can use are beforeend, afterend, beforebegin and afterbegin.

This JavaScript insertAdjacentHTML Method is used in this example as well to insert HTML Content anywhere in the page.

In above JavaScript code we have used afterend, which is used to insert or add HTML content after particular tag or element.

We can also use beforebegin to add same HTML content before that particular HTML tag.

Video Tutorial

Watch video tutorial on how to Insert HTML Code anywhere With JavaScript.

Get Last Element Of Array Without Removing It Remove Last Element From Array JavaScript Remove First Element From Array in JavaScript Tic Tac Toe Game in JavaScript Remove Class from HTML Tag OnClick JavaScript JavaScript Select li Tag By Index Display JavaScript Variable In HTML Insert HTML Code anywhere With JavaScript JavaScript Date Difference in Days JavaScript Hide Button After Click Simple Click and Hold Effect Using JavaScript Remove Class From All Elements in JavaScript JavaScript Select All HTML Elements Whose ID Start With Same String Count Checked Checkboxes of Specific Form with JavaScript Enable and Disable Button using JavaScript Add Class to HTML Tag On Click using JavaScript Change Class of HTML Tag Using JavaScript Get All Options In Select Tag using JavaScript Replace a Word from everywhere in a String using JavaScript Execute a Function on Hover Using JavaScript