title: Adding comma as thousands separator (javascript) - output being deleted instead
body: <p>I am attempting to dynamically adjust a numerical value entered to include thousand separators</p>
  
  <p>Here is my code:</p>
  
  <pre><code>function addCommas(nStr) {
      nStr += '';
      x = nStr.split('.');
      x1 = x[0];
      x2 = x.length &gt; 1 ? '.' + x[1] : '';
      var rgx = /(\d+)(\d{3})/;
      while (rgx.test(x1)) {
          x1 = x1.replace(rgx, '$1' + ',' + '$2');
      }
      return x1 + x2;
  }
  
  
  &lt;input type="number"  onkeyup="this.value=addCommas(this.value);" /&gt;
  </code></pre>
  
  <p>However when I enter numbers after the 4 one, the field is cleared.</p>
  
  <p>Any ideas where I am going wrong? If there is a jQuery solution I'm already using that on my site.</p>