Categories
JavaScript Answers

How to replace text inside a div element with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *