Reputation: 1871
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
Reputation: 148120
You have extra :
in string to search and also assign it back to html of theContent
$(document).ready( function() {
var theContent = $(".transaction-results p").last();
console.log(theContent.html());
theContent.html(theContent.html().replace(/Total/, 'Total without shipping:'));
});
Upvotes: 0
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
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