How To Create & Append HTML Elements In Javascript

Welcome to a quick tutorial on how to create and append HTML elements in Javascript. Need to add some content, or update a certain part of the page using Javascript?

There are 2 common ways to create and insert HTML elements in Javascript.

  1. By directly changing the inner HTML of an element – document.getElementById("ID").innerHTML = "<p>Text</p>";
  2. By creating and appending a new element.
    • var para = document.createElement("p");
    • para.innerHTML = "Text";
    • document.getElementById("ID").appendChild(para);

That should cover the basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes HTML Elements Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

CREATE & APPEND HTML ELEMENTS

All right, let us now get into the examples of how to create and append HTML elements in Javascript.

 

1) BASIC CREATE & INSERT

1-basic.html
<!-- (A) TARGET CONTAINERS -->
<div id="demoA">Demo A</div>
<div id="demoB">Demo B</div>
<div id="demoC">Demo C</div>
 
<!-- (B) CREATE + APPEND ELEMENTS -->
<script>
window.addEventListener("load", function(){
  // (B1) DIRECTLY REPLACE INNER HTML
  document.getElementById("demoA").innerHTML = "<p>Hello</p>";

  // (B2) APPEND INNER HTML
  document.getElementById("demoB").innerHTML += "<p>World</p>";

  // (B3) CREATE + APPEND
  var stonks = document.createElement("strong");
  stonks.innerHTML = "Stonk";
  document.getElementById("demoC").appendChild(stonks);
});
</script>

As in the introduction above, there are 2 ways to modify the HTML – By changing innerHTML and creating a new element. But take extra note of the results and where the insertion happens:

<div id="demoA">Hello</div>
<div id="demoB">Demo B<p>World</p></div>
<div id="demoC">Demo C<strong>Stonk</strong></div>
  • B1 – Yes, innerHTML = "SOMETHING" will totally replace the existing HTML.
  • B2 – To append and not replace, use innerHTML += "SOMETHING" instead.
  • B3TARGET.appendChild(ELEMENT) will always add to the bottom of the target container.

 

 

2) PRECISION INSERT

2-insert.html
<!-- (A) TARGET CONTAINER -->
<div id="demoD">Demo D</div>
 
<!-- (B) JS INSERT -->
<script>
window.addEventListener("load", function(){
  // (B1) CREATE DUMMY ELEMENTS
  var paraA = document.createElement("p"),
      paraB = document.createElement("p"),
      paraC = document.createElement("p"),
      paraD = document.createElement("p");
  paraA.innerHTML = "Before Begin";
  paraB.innerHTML = "After Begin";
  paraC.innerHTML = "Before End";
  paraD.innerHTML = "After End";
 
  // (B2) INSERT
  var target = document.getElementById("demoD");
  target.insertAdjacentElement("beforebegin", paraA);
  target.insertAdjacentElement("afterbegin", paraB);
  target.insertAdjacentElement("beforeend", paraC);
  target.insertAdjacentElement("afterend", paraD);
});
</script>

Need to do some “precision insertion”? Use insertAdjacentElement().

<p>Before Begin</p>
<div id="demoD">
  <p>After Begin</p>
  Demo D
  <p>Before End</p>
</div>
<p>After End</p>
  • beforebegin before the opening tag.
  • afterbegin after the opening tag.
  • beforeend before the closing tag.
  • afterend after the closing tag.

 

 

3) INSERT TO HTML BODY

3-body.html
<!-- (A) DUMMY CONTENT -->
<div>Foo Bar!</div>
<div>Hello World!</div>
 
<!-- (B) JS INSERT -->
<script>
window.addEventListener("load", function(){
  // (B1) CREATE DUMMY ELEMENTS
  var paraA = document.createElement("p"),
  paraB = document.createElement("p");
  paraA.innerHTML = "First";
  paraB.innerHTML = "Last";
 
  // (B2) INSERT
  document.body.insertAdjacentElement("afterbegin", paraA);
  document.body.appendChild(paraB);
});
</script>

Lastly, the HTML body document.body itself is also an element! We also can insert elements into the body.

  • To add an element to the top of the body (top of page) – document.body.insertAdjacentElement("afterbegin", ELEMENT).
  • To append an element to the bottom of the body (bottom of page) – document.body.appendChild(ELEMENT).

P.S. We can also create <script> tags and insert them into document.head.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

Create & Append HTML Elements In Javascript (Click To Enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *