Lewis Day Lewis Day - 5 months ago 14
Javascript Question

Replace specific words amongst page content with value javascript

I'm trying to replace the word

hello
anywhere on the page with the word
hi
using Javascript. I created the script below however it isn't working how anticipated, is there something that I can do to achieve the outcome desired.

function(data) {
var newdata = data.replace("hello", "hi");
}


Jsfiddle

Answer

This will work, although might be overkill using regular expressions:

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

Jsfiddle