How to insert element before element using javascript

Manideepgoud
Posted by Manideepgoud under JavaScript category on | Points: 40 | Views : 233
To insert new element before we have to use insertBefore().
 <div id="home">
<p id="p1">DotNetFunda</p>
<p id="p2">TechFunda</p>
</div>
<script>
var a = document.createElement("p");
var b = document.createTextNode("ITFunda");
a.appendChild(b);
var c = document.getElementById("home");
var child = document.getElementById("p1");
c.insertBefore(a, child);
</script>


insertBefore() is used to insert before the element.

OUTPUT:

ITFunda
DotNetFunda
TechFunda

Comments or Responses

Login to post response