add commas to number without typing in field

add commas to number without typing in field

I have five text fields on a page.  Numbers are entered into the first four and the fifth displays the total.
Is there any way using jquery to format the number in this fifth textfield with commas?

The first four fields are formatted fine using the code below. Of course this is not work on my jsfiddle.  I hate it when that happens but am not worries about that.

My main question is, is there a way to use jquery to format a textfield with numbers using commas, where the number is dynamically generated (i.e. isn't typed in)?


  1. function calculate_expenses() {
  2. document.getElementById("147").value =
  3. parseFloat(document.getElementById("field_1").value.replace(/\,/g,'') || 0) +
  4. parseFloat(document.getElementById("field_2").value.replace(/\,/g,'') || 0) +
  5. parseFloat(document.getElementById("field_3").value.replace(/\,/g,'') || 0) +
  6. parseFloat(document.getElementById("field_4").value.replace(/\,/g,'') || 0);
  7. }
  8. $(function(){
  9. $('#field_1').blur(calculate_expenses);
  10. $('#field_2').blur(calculate_expenses);
  11. $('#field_3').blur(calculate_expenses);
  12. $('#field_4').blur(calculate_expenses);
  13. });
  14.  

Thanks!

Peter T