Sometimes, we want to replace text inside a div element with JavaScript.
In this article, we’ll look at how to replace text inside a div element with JavaScript.
How to replace text inside a div element with JavaScript?
To replace text inside a div element with JavaScript, we set the textContent property of the div element.
For instance, we write
const div = document.querySelector("div");
div.textContent = "New text";
to select a div with querySelector.
Then we set its textContent property to 'New text' to render ‘New text’ as the new content of the div.
Conclusion
To replace text inside a div element with JavaScript, we set the textContent property of the div element.