Home › Forums › JavaScript › Change the text on a wordpress page › Reply To: Change the text on a wordpress page
March 27, 2016 at 5:59 am
#239834
jerryr125
Participant
function replaceTextOnPage(from, to){
getAllTextNodes().forEach(function(node){
node.nodeValue = node.nodeValue.replace(new RegExp(quote(from), ‘g’), to);
});
function getAllTextNodes(){
var result = [];
(function scanSubTree(node){
if(node.childNodes.length)
for(var i = 0; i < node.childNodes.length; i++)
scanSubTree(node.childNodes[i]);
else if(node.nodeType == Node.TEXT_NODE)
result.push(node);
})(document);
return result;
}
function quote(str){
return (str+”).replace(/([.?*+^$[]\(){}|-])/g, “\$1”);
}
}
replaceTextOnPage(‘read more’, ‘View Listing’);