To change the text in a div using JavaScript, we can use the textContent property:

document.getElementById("div1").textContent="newContent";

If your div will contain html content, then you should use the innerHTML property.

document.getElementById("div1").innerHTML = "Paragraph changed!";

Let’s say we have the following HTML:

Today is November 3rd.

If we want to change the text inside the div from “Today is November 3rd.” to “Today is December 3rd.”, we can use the textContent property to do this with the following JavaScript code.

document.getElementById("div1").textContent = "Today is December 3rd.";

We can also use the innerHTML property to change the content of a div.

document.getElementById("div1").innerHTML = "Today is December 3rd.";

While the textContent property is very simple, the innerHTML property gives us the ability to insert html into our div, which gives us more options when styling the content.

Changing the Text in a Div Using JavaScript with a Click

To change the text of a div using JavaScript, we can combine the textContent property with a click event.

Let’s say we have the following HTML, and we want to give the user the ability to change the text of the div from “November” to “December”.

Today is November 3rd.
Update text

We can utilize both the onclick event and the textContent property to change the text from “November” to “December”.

Below is the JavaScript code which will allow the user to be able to update the text in the div:

function clickFunction() {
  var currDiv = document.getElementById("div1");
  currDiv.textContent = "Today is December 3rd.";
}

The final code and output for this example of how to change the text of a div using JavaScript is below:

Code Output:

Today is November 3rd.

Update text

Full Code:

Today is November 3rd.
Update text
<script> function clickFunction() { var currDiv = document.getElementById("div1"); currDiv.textContent = "Today is December 3rd."; } </script>

Using the innerHTML Property to Change Text in a Div

We can use the innerHTML property to change the html and text of a div.

Let’s say we have the following code and we want to give the user the ability to add HTML content to the div.

This is original text.
Make bold

If we want to make the text “content” bold, we can do this easily utilizing both the onclick event and the innerHTML property.

Below is the JavaScript code which will allow the user to change the content in the div.

function clickFunction() {
  var currDiv = document.getElementById("div2");
  currDiv.innerHTML = "This text is now bold.";
}

The final code and output for this example are below:

Code Output:

This is original text.

Make bold

Full Code:

This is original text.
Make bold
<script> function clickFunction2() { var currDiv = document.getElementById("div2"); currDiv.innerHTML = "This text is now bold."; } </script>

Hopefully this article has been useful for you to understand how to change text in a div using JavaScript.

Categorized in:

JavaScript,

Last Update: February 26, 2024