I want to replace all commas of a (number) string with dots and add another element at the same time to display the currency
so far I have this
$("#myelement").text(function () {
     return $(this).text().replace(/\,/g, '.');
});
So far this works and returns for example 1,234,567 as 1.234.567 but how can I add a string/element to it so that I get 1.234.567 Dollars or 1.234.567 Rupis etc.
Just add + " Dollars" (or Rupees, etc.) to what you're returning from the function:
$("#myelement").text(function () {
     return $(this).text().replace(/\,/g, '.') + " Dollars";
});
Note that as georg points out, you don't need the $(this).text() part, the callback gets the index and the old text as arguments:
$("#myelement").text(function(index, text) {
     return text.replace(/\,/g, '.') + " Dollars";
});
Side note: , isn't special in regular expressions, no need to escape it (although doing so is harmless). So just /,/g, not /\,/g.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With