Replace commas with dots and add an item to the line

I want to replace all comma strings (numbers) with dots and at the same time add another element to display the currency

I still have it

$("#myelement").text(function () {
     return $(this).text().replace(/\,/g, '.');
});

So far this works and returns, for example, 1,234,567how 1.234.567, but how can I add a line / element to it to get 1.234.567 Dollarsor 1.234.567 Rupisetc.

+4
source share
1 answer

Just add + " Dollars"(or Rupees, etc.) to what you return from the function:

$("#myelement").text(function () {
     return $(this).text().replace(/\,/g, '.') + " Dollars";
});

Note that as georg points out , you don’t need the part $(this).text(), the callback gets the index and old text as arguments:

$("#myelement").text(function(index, text) {
     return text.replace(/\,/g, '.') + " Dollars";
});

: , , ( ). /,/g, /\,/g.

+7

Source: https://habr.com/ru/post/1629875/


All Articles