-2

I am working on a project at work where I want to format values on a table from (128879) to look like this (128,879). The code I have used so far is:

$(document).ready(function(){
$('#tableOne tr td:nth-child(3)').each(function(){
    var num = $(this).text;
    num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}); });

Also, I tried the jQuery NumberFormatter Library, but an error code shows that jQuery is not defined (the last line of code on the debugger page using Chrome) and Math.js (no luck). Not sure why this is happening.

The <script> tag used for the jQuery and Math.js is shown below:

<script type = 'text/javascript' src ="jquery.numberformatter-1.2.4.min.js"></script>
<script type="text/javascript" src="math.js"></script>
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
fretigason2
  • 39
  • 1
  • 8
  • `num.toString().replace(`…`);` by itself doesn’t actually update anything. It should probably be `$(this).text = num.toString().replace(`…`);`. – Sebastian Simon Aug 23 '16 at 22:14
  • Possible duplicate of [Replace method doesn't work](http://stackoverflow.com/questions/1433212/replace-method-doesnt-work) – Sebastian Simon Aug 23 '16 at 22:15

1 Answers1

0

You need to include the jQuery library so you can use $ like you want. Also, $(this).text is a function; you want num = $(this).text() and $(this).text(num.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")). I have not tested your regex, so I don't know if it would work.

csander
  • 1,385
  • 10
  • 11