0

I am using this method to find and replace a piece of text and not sure why it is not working? When I use console.log, I can see the correct content I want to replace but the end result is not working:

(function($) {
  $(document).ready( function() {
        var theContent = $(".transaction-results p").last();
        console.log(theContent.html());
        theContent.html().replace(/Total:/, 'Total without shipping:');
    });
})(jQuery);

Any thoughts?

Thank you!

2
  • string.replace returns a string - it doesn't do the replace on the string you reference... theContent.html(theContent.html().replace(/Total:/, 'Total without shipping:')); Commented Nov 1, 2012 at 12:14
  • @diEcho This isn't PHP, you don't wrap regex in quotes. Commented Nov 1, 2012 at 12:16

3 Answers 3

3

The string was replaced, but you didn't reassign the string to the html of the element. Use return:

theContent.html(function(i,h){
    return h.replace(/Total:/, 'Total without shipping:');
});

JS Fiddle demo (kindly contributed by diEcho).

References:

Sign up to request clarification or add additional context in comments.

Comments

0

You have extra : in string to search and also assign it back to html of theContent

Live Demo

  $(document).ready( function() {
        var theContent = $(".transaction-results p").last();
        console.log(theContent.html());
        theContent.html(theContent.html().replace(/Total/, 'Total without shipping:'));
  });

Comments

0
(function($) {
  $(document).ready( function() {
        var theContent = $(".transaction-results p").last();
        console.log(theContent.html());
        theContent.html(theContent.html().replace('Total:', 'Total without shipping:'));
    });
})(jQuery);

Why you did /Total:/ and not 'Total' like a normal string?

-The solution from @David Thomas works.

1 Comment

Because he was using regular expressions, not strings.

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.