Note that this will replace the first instance of hello throughout the body, including any instances in your HTML code (e.g. class names etc..), so use with caution - for better results, try restricting the scope of your replacement by targeting your code using document.getElementById or similar.
To replace all instances of the target string, use a simple regular expression with the global flag:
// Note this *is* JQuery, see below for JS solution instead
function replaceText(selector, text, newText, flags) {
var matcher = new RegExp(text, flags);
$(selector).each(function () {
var $this = $(this);
if (!$this.children().length)
$this.text($this.text().replace(matcher, newText));
});
}
Here's a usage example:
function replaceAllText() {
replaceText('*', 'hello', 'hi', 'g');
}
$(document).ready(replaceAllText);
$('html').ajaxStop(replaceAllText);
But be careful with it since it may affect the tags, css and scripts as well.
EDIT:
As for a pure JavaScript solution use this method instead:
function replaceText(selector, text, newText, flags) {
var matcher = new RegExp(text, flags);
var elems = document.querySelectorAll(selector), i;
for (i = 0; i < elems.length; i++)
if (!elems[i].childNodes.length)
elems[i].innerHTML = elems[i].innerHTML.replace(matcher, newText);
}
Try to apply the above suggested solution on pretty big document, replacing pretty short strings which might be present in innerHTML or even innerText, and your html design becomes broken at best
Therefore I firstly pickup only text node elements via HTML DOM nodes, like this
function textNodesUnder(node){
var all = [];
for (node=node.firstChild;node;node=node.nextSibling){
if (node.nodeType==3) all.push(node);
else all = all.concat(textNodesUnder(node));
}
return all;
}
textNodes=textNodesUnder(document.body)
for (i in textNodes) { textNodes[i].nodeValue = textNodes[i].nodeValue.replace(/hello/g, 'hi');
`and followingly I applied the replacement on all of them in cycle
While using innerHTML.replace will work and is pretty fast, this will break the DOM state.
You can replicate this by setting "autofocus" on an input field and then changing innerHTML on body, it will loose focus.
A less destructive approach is:
// retrives all childNodes of body
var childNodes = document.body.childNodes;
// start replacing
replaceInNodes(childNodes, "search", "replace");
function replaceInNodes(nodes,search,replace) {
// iterate through all nodes
for (var i = 0; i < nodes.length; i++) {
var curNode = nodes[i];
// if the node has attributes, let us look at those
// i.e. we want to change "John" in the input placeholder to "Peter" - <input type="text" value="John">
if (curNode.attributes !== undefined) {
var curNodeAttributes = curNode.attributes;
for (var ii = 0; ii < curNodeAttributes.length; ii++) {
// replace attribute values
curNodeAttributes[ii].nodeValue = curNodeAttributes[ii].nodeValue.replace(search, replace);
}
}
// It is a "TEXT_NODE"
// i.E. <span>John</span>
if (curNode.nodeType === Node.TEXT_NODE) {
curNode.data = this.injectIntoString(curNode.data);
}
// It is a "ELEMENT_NODE", meaning we need to go deeper
if (curNode.nodeType === Node.ELEMENT_NODE) {
this.replaceInNodes(curNode.childNodes);
}
}
}