SixfootJames
SixfootJames

Reputation: 1871

jQuery replace text not working

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!

Upvotes: 0

Views: 499

Answers (3)

Adil
Adil

Reputation: 148120

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:'));
  });

Upvotes: 0

David Thomas
David Thomas

Reputation: 253318

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:

Upvotes: 3

ioan
ioan

Reputation: 771

(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.

Upvotes: 0

Related Questions