# Thread: Sum Value From Form Fields

1. ## Sum Value From Form Fields

Hello,
I am working on a script, but I'm really not an expert. Any help will be highly appreciated.
Description:
I have items - some have option for adding an amount and some not (because they can be only 1).
I need when the checkboxes are clicked (choosing an item) the total to be displayed in the text field at the bottom.
the single items work fine, but when i choose the one that can be added amount, it deletes the total from the first ones.
Below is the script so you can see what I'm trying to explain:

Code:
```<html><head>
<script type="text/JavaScript"><!--

var Cost;
var Amount = 0;

function getNumber() {
Amount = document.orderform.EVT_01.value;
}
function tally()        {
Cost = 0;
if (document.orderform.Item1.checked) { Cost = Cost + 40; }
if (document.orderform.Item2.checked) { Cost = Cost + 30; }
if (document.orderform.Item3.checked) { Cost = 100 * Amount; }

Cost = dollar(Cost);
document.orderform.Total.value = "\$" + Cost;
}

function dollar (amount)
{
amount = parseInt(amount * 100);
amount = parseFloat(amount/100);
if (((amount) == Math.floor(amount)) && ((amount - Math.floor (amount)) == 0))
{
amount = amount + ".00"
return amount;
}
if ( ((amount * 10) - Math.floor(amount * 10)) == 0)
{
amount = amount + "0";
return amount;
}
if ( ((amount * 100) - Math.floor(amount * 100)) == 0)
{
amount = amount;
return amount;
}
return amount;
}

//--></script>
<body><form name="orderform">
<tr><td width="167"><input type="checkbox" name="Item1" value="Item1_chosen" onClick="tally()" />AAA</td>
<td width="51">\$40</td>
<td width="109">&nbsp;</td></tr>
<tr><td><input type="checkbox" name="Item2" value="Item2_chosen" onClick="tally()" />BBB</td>
<td>\$30</td>
<td>&nbsp;</td></tr>
<tr><td colspan="3">&nbsp;</td></tr>
<tr><td><input type="checkbox" name="Item3" value="Item3_chosen" onClick="tally()" /><span class="EventPrice"><a href="#" title=" CCC "

target="_blank">CCC</a></span></td>
<td><span class="EventPrice">\$100</span></td>

<td>Amount:<input name="EVT_01" type="text" id="EVT_01" value="0" size="3" maxlength="3" onChange="getNumber()" /></td></tr>
<tr>
<td colspan="3">&nbsp;</td></tr>
<td colspan="2">&nbsp;</td></tr>
</table></form></body></html>```

2. Should the third checkbox code be
Code:
`Cost = 100 * Amount + Cost;`
Also, is your dollar function just to get 2 decimal places? If so, use
Code:
`Cost = Cost.toFixed(2);`

3. Thanks, r51,
It works!
Look, I have one small issue:
you have to put the amount first and then check the box (for CCC item) to work fine.
I need to be the opposite way - first the user will check the box and then when he add the amount it should change the total.
Also if you change the amount later it doesn't refresh the total until you don't uncheck and check the CCC box again.
Can you help me with this?

4. Rather than use onclick on the checkboxes, use onchange on the text box.

5. I changed it but it doesn't make any difference.
What I like is when first check the box, and then add the amount it automatically to change the total (without need to click somewhere else to refresh the total).
Is this possible?

6. You can try onkeypress or onkeydown or onkeyup on the text box.

7. The same result ...
But however - The important part has been solved.
Thank you!

by the way is there a way that i can place a button or other element, which will cause refreshing the total text field?
Something like "Confirm" button...?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•