I am trying to make an increment/idle game that constantly has changing values in terms of money. I want to be able to separate large numbers using commas. E.g 1000 becomes 1,000 and so on, all while the value is till changing.

```
$<span id="money">0</span>

```

Above shows how I am using the span tag to call the money variable from the javascript. How would I make sure that this money variable stays formatted constantly even when changing?

**Edit**

```
function formatNumber(e){
var rex = /(^\d{2})|(\d{1,3})(?=\d{1,3}|$)/g,
  val = this.value.replace(/^0+|\.|,/g,""),
  res;

if (val.length) {
res = Array.prototype.reduce.call(val, (p,c) => c + p)            // reverse the pure numbers string
           .match(rex)                                            // get groups in array
           .reduce((p,c,i) => i - 1 ? p + "," + c : p + "." + c); // insert (.) and (,) accordingly
res += /\.|,/.test(res) ? "" : ".0";                              // test if res has (.) or (,) in it
this.value = Array.prototype.reduce.call(res, (p,c) => c + p);    // reverse the string and display
  }
}

var mySpan = document.getElementById("money"); 
mySpan.addEventListener("change", formatNumber);

```

I have now implemented this code into my javascript but it still does not seem to be updating the variable.

**Edit 2**

```
function moneyClick(number){
money = money + number;
document.getElementById("money").innerHTML = money;
lifetimeearnings = lifetimeearnings + number;
document.getElementById("lifetimeearnings").innerHTML = lifetimeearnings; 

```

};