-1

Is it possible to sum like Ms Excel on html with jquery? And how about if there is a string inside like this <td>USD 7.50</td>, is it possible?

jsfiddle link.

Sorry for my english btw.

Community
  • 1
  • 1
Harriz
  • 77
  • 3
  • 10

2 Answers2

1

You can make sum with a each of columns

var count=0;
$(".number").each(function(){
    count=count+parseInt($(this).html().replace("USD","").trim());
});
$(".footer-table tr td:last").html(count);
LTasty
  • 2,008
  • 14
  • 22
0

If your column might have other text besides USD or possibly comas, you might consider a regex to replace everything that is not a number or a period.

Note that something like G6 900.18, would be seen as 6900.18 so be aware of that.

jsFiddle

var count=0;
$(".number").each(function(){
    count = count + $(this).html().replace(/[^.\d]/g,'') * 1000;
});
$(".grand-total").html((count / 1000).toFixed(2));
Community
  • 1
  • 1
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • i have no idea what this `/[^.\d]/g` mean, and why multiple 1000? – Harriz Aug 29 '15 at 15:28
  • 1
    That is a regular expression that matches all characters that are not a digit or a period. Basically, it'll get rid of anything that is not 0-9 or a period which would be more useful if for example, you may sometimes show the amounts in a different currency. The multiplying by 1000 then dividing by 1000 is to avoid unexpected behaviour that can occur when doing math with floats in `javascript` see [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) for more info. If you know you'll never have decimal numbers, you wont need that. – Wesley Smith Aug 29 '15 at 15:38