Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Change the text on a wordpress page Reply To: Change the text on a wordpress page

#239835
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’);