I’m using this script to sum input fields based on class and return the result to a specified id.
This works perfectly although there are two functionalists I’m struggling with:
.replace(',','')); can only cope with one commas, ie 1,000 but not with multiple 1,000,000
I would like the returned result to put back commas every third number, .replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"); does nothing?
$(document).ready(function () {
$(this).keyup(function () {
var sum = 0;
var column2 = $('.left')
jQuery.each(column2, function (number) {
sum += parseFloat($(this).val().replace(',', ''));
});
$('#total').val(sum).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
});
});
I have goggled and tried to find an answer but can’t find anything that works.
Many thanks,
Sam
You can do this using the following code
Working fiddle