I’d like to add two amounts together, each one from a different element, and put them in an other element. The tricky part is to keep the “$” sign and comma (,) in the total amount (or maybe add them to the total amount afterwards?).
Anyone know of a Javascript that can do this?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add amounts and put in element</title>
</head>
<body>
<p id="firstAmount">$1,133.79</p>
<p id="secondAmount">$1,900.00</p>
<br />
Total: <p id="totalAmount">$0.00</p>
</body>
</html>
******************** UPDATE 2 **********************
Replace:
var total2=addCommasandsign(total);
by:
var total2=addCommasandsign(total.toFixed(2));
This fixes the problem of zeros disappearing after the decimal point. If for example you had $1,133.00 + $1,900.00 you would see $3,033
With this fix you’ll see the zeros after the decimal point => $3,033.00
You can see the live example here
******************** UPDATE 1 **********************
You can find a live example of the working code here
The following should work but didn’t test it: