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

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #239833
    jerryr125
    Participant

    hi – simple – looking for javascript that would change the text on wordpress page from “read more” to “view listing” ?

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

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

    #239839
    Senff
    Participant

    I suggest you don’t use Javascript for that, but the “proper” WordPress method instead: https://codex.wordpress.org/Customizing_the_Read_More

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.