add text to div javascript code example

Example 1: javascript set text of div

document.getElementById("content").innerHTML = "whatever";

Example 2: JavaScript append text to div

var div = document.getElementById('myElementID');
div.innerHTML += "Here is some more data appended";

Example 3: 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 4: how add text to element in javascript

var paragraph = document.getElementById("p");
var text = document.createTextNode("This just got added");

paragraph.appendChild(text);

Example 5: how to append in javascript

var list=[1, 2, 3, 4, 5];
list.push(6);
// .push allows you to add a value to the end of a list

Example 6: how add text to element in javascript

document.getElementById("p").textContent += " this has just been added";