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 Heading Text using JavaScript Current Date in mm/dd/yyyy Format JavaScript Current Date in dd/mm/yyyy Format in JavaScript JavaScript Current Date in yyyy-mm-dd Format JavaScript Get Date Difference in Hours JavaScript Get Date Difference in Minutes JavaScript Get Date Difference in Seconds Get Last Character of String with JavaScript Trigger Button Click On Page Load JavaScript Get id Of Child Element JavaScript Get Id Of Parent Element in JavaScript Get Table Row Index onclick in JavaScript Remove specific option from Select Tag with JavaScript Change Selected Option of Select Tag with JavaScript Get Length of Text In Textarea with JavaScript Change Text Color of Select Tag Option using JavaScript Change Heading Text in JavaScript Get Input Value Length in JavaScript Toggle Button Text with JavaScript Change Background Color of Dropdown with Dropdown using JavaScript Emoji Mood Slider with HTML CSS and JavaScript Hide and Show Div with Radio Button using JavaScript Count Options In Select Tag with JavaScript Get Select Tag Text OnChange with JavaScript Change Background Color OnClick using Input Field