0

I must've tried half a dozen scripts (most found here, e.g. at jQuery find and replace string) to replace text on my Wordpress-built site (http://www.sehkelly.com/).

None work. I'm not clever enough to diagnose why (but not stupid enough not to know how to run a script). Into header.php the script goes, in the usual way, and no result.

For instance, all instance of "Shop" on my homepage (in the menu, the h2 elements, in the Wordpress content) remain as such, despite this script ...

$("span, p, div").each(function() {
    var text = $(this).text();
    text = text.replace("Shop", "Sale");
    $(this).text(text);
});

Any ideas?

I have disabled caching plugins to no avail.

Thanks in advance.

UPDATE

In full I have ...

<script type="text/javascript">
    $(document).ready(function(){
        $("span, p, div").each(function() {
        var text = $(this).text();
        text = text.replace("type", "typo");
        $(this).text(text);
    });
</script>

Still no joy.

3
  • Just try to print the values in the console.Just check weather you are getting values in console.Where is your script in document.ready ? Commented Mar 5, 2016 at 10:51
  • Did you call that script in document ready $(function() { ... }); Commented Mar 5, 2016 at 10:53
  • text = text.replace(new RegExp("Shop",'g'), "Sale"); Commented Mar 5, 2016 at 10:53

3 Answers 3

1

If you execute such code on your website it will create a mess in your html...Don't do that :)

Do this instead:

$("span, p, div").each(function() {
    var text = $(this).html();
    text = text.replace("Shop", "Sale");
    $(this).html(text);
});
Sign up to request clarification or add additional context in comments.

Comments

1

You need to call this in document ready and use regex to replace all instances:

$(function(){
    $("span, p, div").each(function() {
        var text = $(this).text();
        text = text.replace(/Shop/g, "Sale"); // regex instead of string
        $(this).text(text);
    });
});

Comments

0

Use this code and then you will be happy

<script type="text/javascript">
    $(document).ready(function(){
    var replaced = $("body").html().replace('Shop','Sale');
    $("body").html(replaced);
    });
</script>

1 Comment

That's the one. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.