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!
asked Nov 1, 2012 at 12:12
SixfootJames
1,8716 gold badges26 silver badges42 bronze badges
3 Answers 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:
answered Nov 1, 2012 at 12:15
David Thomas
254k53 gold badges382 silver badges421 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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:'));
});
answered Nov 1, 2012 at 12:16
Adil
148k25 gold badges217 silver badges207 bronze badges
Comments
(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.
answered Nov 1, 2012 at 12:15
ioan
7711 gold badge9 silver badges27 bronze badges
1 Comment
David Thomas
Because he was using regular expressions, not strings.
lang-js
theContent.html(theContent.html().replace(/Total:/, 'Total without shipping:'));