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 = "<span>Paragraph changed!</span>";
Let’s say we have the following HTML:
<div id="div1">Today is November 3rd.</div>
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 <span>December</span> 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”.
<div>
<div id="div1">Today is November 3rd.</div>
<div id="click-me" onclick="clickFunction()">Update text</div>
</div>
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:
Full Code:
<div>
<div id="div1">Today is November 3rd.</div>
<div id="click-me" onclick="clickFunction()">Update text</div>
</div>
<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.
<div>
<div id="div2">This is original text.</div>
<div class="click-me" onclick="clickFunction2()">Make bold</div>
</div>
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 = "<b>This text is now bold.</b>";
}
The final code and output for this example are below:
Code Output:
Full Code:
<div>
<div id="div2">This is original text.</div>
<div class="click-me" onclick="clickFunction2()">Make bold</div>
</div>
<script>
function clickFunction2() {
var currDiv = document.getElementById("div2");
currDiv.innerHTML = "<b>This text is now bold.</b>";
}
</script>
Hopefully this article has been useful for you to understand how to change text in a div using JavaScript.
Leave a Reply