javascript add element on document code example

Example 1: javascript append to paragraph

// In the JS script

var parElement = document.getElementById("myPar");
var textToAdd = document.createTextNode("Text to be added");
parElement.appendChild(textToAdd);

//In the HTML file

<p id="myPar"></p>

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