You can use the innerHTML property to add text to a div element using JavaScript. Here’s an example of how you can do it:

// Get the div element
var div = document.getElementById("myDiv");
// Append text to the div
div.innerHTML += "This is some new text.";

In this example, we first use the getElementById method to get a reference to the div element, and then we use the ‘+=’ operator to append the text to the existing content of the div.

You can also use textContent property to add text to a div element. Here’s an example of how you can do it:

// Get the div element
var div = document.getElementById("myDiv");
// Append text to the div
div.textContent += "This is some new text.";

This will also add text to the DIV but it also add text content as a text node and it doesn’t evaluate any HTML tags.

Also Read:

Categorized in: