javascript add html tag code example

Example 1: code for adding new elements in javascriipt js

<html> 
<head> 
<title>t1</title> 
<script type="text/javascript"> 
	function addNode() 
     {var newP = document.createElement("p"); 
	  var textNode = document.createTextNode(" This is a new text node"); 
	  newP.appendChild(textNode);
      document.getElementById("firstP").appendChild(newP); } 
</script> </head> 
<body> <p id="firstP">firstP<p> </body> 
</html>

Example 2: js add more text to element

document.getElementById("p").textContent += " This is the text from javascript.";

<p id ="p">This is the text from HTML.</p>

Tags:

Html Example