How to Inject HTML with JavaScript?

Estimated read time 1 min read

To inject HTML with JavaScript, you can use the following methods:

  1. Using innerHTML property:
document.getElementById("target").innerHTML = "<p>Hello World!</p>";
  1. Using createElement() and appendChild() methods:
var para = document.createElement("p");
var node = document.createTextNode("Hello World!");
var element = document.getElementById("target");
  1. Using insertAdjacentHTML() method:
document.getElementById("target").insertAdjacentHTML("beforeend", "<p>Hello World!</p>");

Note: In these examples, “target” is the id of the element where you want to inject the HTML.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply