How to replace words in the body text with JavaScript?

Spread the love

To replace words in the body text with JavaScript, we call the replace method.

For instance, we write

document.body.innerHTML = document.body.innerHTML.replace("hello", "hi");

to call document.body.innerHTML.replace with 'hello' and 'hi' to replace 'hello' with 'hi' in the body element.

And then we assign the replaced string back to document.body.innerHTML to update the body element’s contents with the new string.

Leave a Reply

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