Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd amounts and put in element
    text
    copied!<p>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?).</p> <p>Anyone know of a Javascript that can do this?</p> <p><br /></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Add amounts and put in element&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;p id="firstAmount"&gt;$1,133.79&lt;/p&gt; &lt;p id="secondAmount"&gt;$1,900.00&lt;/p&gt; &lt;br /&gt; Total: &lt;p id="totalAmount"&gt;$0.00&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><br /></p> <p><br /></p> <p><em><strong></em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em></strong> UPDATE 2 <strong></em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>****</strong> </p> <p>Replace:</p> <pre><code>var total2=addCommasandsign(total); </code></pre> <p>by:</p> <pre><code>var total2=addCommasandsign(total.toFixed(2)); </code></pre> <p><br /> 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 <br /> With this fix you'll see the zeros after the decimal point => $3,033.00</p> <p>You can see the live example <a href="http://jsfiddle.net/LGNwS/2/" rel="nofollow">here</a></p> <p><br /></p> <p><em><strong></em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em></strong> UPDATE 1 <strong></em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>****</strong> </p> <p>You can find a live example of the working code <a href="http://jsfiddle.net/LGNwS/1/" rel="nofollow">here</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload