Click to See Complete Forum and Search --> : How do I add all the calculated fields together.
DarryBoy
04-24-2003, 04:56 PM
Hi Jona
How do I add all the results together to give me an added total in the total field?
When I enter a number in the first field it shows up a Nan in the calculated field. How do I reflect a $0 value until one of the checkboxes are ticked?
Here is your code with a subtle difference.
<html><head>
<script>
function calc(qty, total, box1, box2){
var price;
if(box1.checked){price=box1.value;}
if(box2.checked){price=box2.value;}
total.value=parseInt(qty)*price;
if(total.value.indexOf("$")){
total.value="$"+total.value+".00";}
}
function chk(box1, box2){
var count = 0;
if(box1.checked){count+=1;}
if(box2.checked){count+=1;}
if(count==2){alert("Check only one, please."); return false;}
}
</script>
</head><body>
<form name="qtyFrm">
<table width="75%" border="0">
<tr>
<td><input type=text name="qty" onKeyDown="calc(this.value,this.form.qtyTotal,this.form.c1,this.form.c2)" onKeyUp="calc(this.value,this.form.qtyTotal,this.form.c1,this.form.c2)"></td>
<td> 130:
<input type=checkbox name="c1" value="130" onClick=" calc(this.form.qty.value,this.form.qtyTotal,this,this.form.c2); return chk(this,this.form.c2);">
1300:
<input type=checkbox name="c2" value="1300" onClick="calc(this.form.qty.value,this.form.qtyTotal,this.form.c1,this); return chk(this.form.c1,this);"></td>
<br>
<td><input type=text name="qtyTotal"></td>
</tr>
<tr>
<td><input name="qty2" type="text" id="qty2" onKeyDown="calc(this.value,this.form.qtyTotal2,this.form.c3,this.form.c4)" onKeyUp="calc(this.value,this.form.qtyTotal2,this.form.c3,this.form.c4);"></td>
<td> 130:
<input name="c3" type="checkbox" id="c3" value="130" onClick="calc(this.form.qty2.value,this.form.qtyTotal2,this,this.form.c4); return chk(this,this.form.c4);">
1300:
<input name="c4" type="checkbox" id="c42" value="1300" onClick="calc(this.form.qty2.value,this.form.qtyTotal2,this.form.c3,this); return chk(this.form.c3,this);"></td>
<td><input name="qtyTotal2" type="text" id="qtyTotal2"></td>
</tr>
<tr>
<td><input type="text" name="textfield3" onKeyDown="calc(this.value,this.form.textfield4,this.form.checkbox3,this.form.checkbox4)" onKeyUp="calc(this.value,this.form.textfield4,this.form.checkbox3,this.form.checkbox4)"></td>
<td> 130:
<input type="checkbox" name="checkbox3" value="180" onClick=" calc(this.form.textfield3.value,this.form.textfield4,this,this.form.checkbox4); return chk(this,this.form.checkbox4);">
1300:
<input type="checkbox" name="checkbox4" value="1800" onClick=" calc(this.form.textfield3.value,this.form.textfield4,this.form.checkbox3,this); return chk(this.form.checkbox3,this);"></td>
<td><input type="text" name="textfield4"></td>
</tr>
<tr>
<td> </td>
<td><div align="right"><strong>Total</strong></div></td>
<td><input name="total" type="text" id="total"></td>
</tr>
</table>
</form></body></html>
function calc(qty, total, box1, box2){
var price;
if(box1.checked){price=box1.value;}
if(box2.checked){price=box2.value;}
total.value=parseInt(qty)*price;
if(NaN(total.value)){total.value="$0";}
if(total.value.indexOf("$")){
total.value="$"+total.value+".00";}
}
DarryBoy
04-24-2003, 05:21 PM
I get a function expected error with the new function Jona.
Just add this part of the code where shown in my last post. Don't copy and paste the whole thing! :D
if(NaN(total.value)){total.value="$0";}
DarryBoy
04-24-2003, 05:28 PM
I did only add that part in Jona and it gives a error.
if(isNaN(total.value)){total.value="$0";}
Forgot the "is" ....:p
DarryBoy
04-24-2003, 05:43 PM
Just a little difference like that solves the problem. Amazing.
Jona I have a new field called total in your code at the bottom. How do I add all these calculated fields together to give me the total?
Jona I have a new field called total in your code at the bottom. How do I add all these calculated fields together to give me the total?
Say what? Sorry, I didn't quite follow.... We already have a totalQTY field....
DarryBoy
04-24-2003, 05:57 PM
If you look at the code I have added a new textfield called total (maybe I should have called it something else).
The calculation works spot on. I want to add the totalQty to the next and next to give me a added total.
If you look at the code I'm sure you will follow me.
Oh, I see... Hmmm.... Hold on a second let me see what I can come up with. :D
Going back to my original (I'm sure you can change this up to fit your needs):
<html><head>
<script>
function calc(qty, total, box1, box2){
var price;
var complete = document.qtyFrm.complete;
if(box1.checked){price=box1.value;}
if(box2.checked){price=box2.value;}
total.value=parseInt(qty)*price;
if(total.value.indexOf("$")){
total.value="$"+total.value+".00";}
}
function addAll(){
document.qtyFrm.complete.value=document.qtyFrm.qtyTotal.value+document.qtyFrm.qtyTotal2.value+docume nt.qtyFrm.textbox4.value;
}
function chk(box1, box2){
var count = 0;
if(box1.checked){count+=1;}
if(box2.checked){count+=1;}
if(count==2){alert("Check only one, please."); return false;}
}
</script>
</head><body>
<form name="qtyFrm">
<p>
<input type=text name="qty" onKeyDown="calc(this.value,this.form.qtyTotal,this.form.c1,this.form.c2)" onKeyUp="calc(this.value,this.form.qtyTotal,this.form.c1,this.form.c2);addAll();">
<br>
130:
<input type=checkbox name="c1" value="130" onclick=" calc(this.form.qty.value,this.form.qtyTotal,this,this.form.c2); return chk(this,this.form.c2);">
<br>
1300:
<input type=checkbox name="c2" value="1300" onclick="calc(this.form.qty.value,this.form.qtyTotal,this.form.c1,this); return chk(this.form.c1,this);">
<br>
<input type=text name="qtyTotal">
</p>
<p>
<input name="qty2" type="text" id="qty2" onKeyDown="calc(this.value,this.form.qtyTotal2,this.form.c3,this.form.c4)" onKeyUp="calc(this.value,this.form.qtyTotal2,this.form.c3,this.form.c4); addAll();">
<br>
130:
<input name="c3" type="checkbox" id="c3" value="130" onclick="calc(this.form.qty2.value,this.form.qtyTotal2,this,this.form.c4); return chk(this,this.form.c4);">
<br>
1300: <input name="c4" type="checkbox" id="c4" value="1300" onclick="calc(this.form.qty2.value,this.form.qtyTotal2,this.form.c3,this); return chk(this.form.c3,this);">
<br>
<input name="qtyTotal2" type="text" id="qtyTotal2">
</p>
<p>
<input type="text" name="textfield3" onkeydown="calc(this.value,this.form.textfield4,this.form.checkbox3,this.form.checkbox4)" onkeyup="calc(this.value,this.form.textfield4,this.form.checkbox3,this.form.checkbox4); addAll();">
<br>
130:
<input type="checkbox" name="checkbox3" value="180" onclick=" calc(this.form.textfield3.value,this.form.textfield4,this,this.form.checkbox4); return chk(this,this.form.checkbox4);">
<br>
1300:
<input type="checkbox" name="checkbox4" value="1800" onclick=" calc(this.form.textfield3.value,this.form.textfield4,this.form.checkbox3,this); return chk(this.form.checkbox3,this);">
<br>
<input type="text" name="textfield4">
<br>
</p>
<input type=text name="complete">
</form></body></html>
DarryBoy
04-24-2003, 06:28 PM
This is the result shown in the complete text field when you click on the third checkbox.
$260.00$NaN.00$NaN.00
How do I reflect the value of the first checkbox when I click on it and it will add the value when I click on the third and fifth?
I have and continue to learn from you. I thank and admire you.
Oh boy.... Okay, let's see. That wasn't a dynamic function, but I got a mix-up there... The $ messes it up.. So... Hmmm... I know, here, try this:
function addAll(){
var all = document.qtyFrm.qtyTotal.value.split("$").join("");
all = parseFloat(all);
all = all+document.qtyFrm.qtyTotal2.value.split("$").join("");
all = parseFloat(all);
all = all+document.qtyFrm.textbox4.value.split("$").join("");
all = parseFloat(all);
document.qtyFrm.complete.value="$"+all;
}
DarryBoy
04-24-2003, 06:47 PM
This works brilliantly. How quickly do you think I could learn JavaScript?
As quickly as I did, possibly. Took me .. Since May last year.. I didn't count the months...
DarryBoy
04-24-2003, 07:41 PM
I will try my best to learn as fast as you.
There two things the addall function doesn't do. When you enter a value in the first field and then click on one of the checkboxes it doesnt show a value which makes since as its not adding anything. I need it to reflect the value based on which checkbox is clicked and then add the other fields.
When I click on the checkbox it correctly adds the values and displays it in the complete field but if you uncheck the checkboxes the values it added are still shown. It should show 0 when the checkboxes are unchecked.
Please run the app if you dont follow.
I understand. I'll work it out...
I love originality.....
<html><head>
<script>
function calc(qty, total, box1, box2){
var price;
if(box1.checked){price=box1.value;}
if(box2.checked){price=box2.value;}
complete=total.value=parseInt(qty)*price;
if(total.value.indexOf("$")){
total.value="$"+total.value+".00";}
var a = document.qtyFrm.qtyTotal.value.split("$").join("");
b = document.qtyFrm.qtyTotal2.value.split("$").join("");
c = document.qtyFrm.textfield4.value.split("$").join("");
all = parseFloat(a*1+b*1+c*1)
document.qtyFrm.complete.value="$"+all+".00";
}
function chk(box1, box2){
var count = 0;
if(box1.checked){count+=1;}
if(box2.checked){count+=1;}
if(count==2){alert("Check only one, please."); return false;}
}
</script>
</head><body>
<form name="qtyFrm">
<p>
<input type=text name="qty" onKeyDown="calc(this.value,this.form.qtyTotal,this.form.c1,this.form.c2)" onKeyUp="calc(this.value,this.form.qtyTotal,this.form.c1,this.form.c2);">
<br>
130:
<input type=checkbox name="c1" value="130" onclick=" calc(this.form.qty.value,this.form.qtyTotal,this,this.form.c2); return chk(this,this.form.c2);">
<br>
1300:
<input type=checkbox name="c2" value="1300" onclick="calc(this.form.qty.value,this.form.qtyTotal,this.form.c1,this); return chk(this.form.c1,this);">
<br>
<input type=text name="qtyTotal">
</p>
<p>
<input name="qty2" type="text" id="qty2" onKeyDown="calc(this.value,this.form.qtyTotal2,this.form.c3,this.form.c4)" onKeyUp="calc(this.value,this.form.qtyTotal2,this.form.c3,this.form.c4); ">
<br>
130:
<input name="c3" type="checkbox" id="c3" value="130" onclick="calc(this.form.qty2.value,this.form.qtyTotal2,this,this.form.c4); return chk(this,this.form.c4);">
<br>
1300: <input name="c4" type="checkbox" id="c4" value="1300" onclick="calc(this.form.qty2.value,this.form.qtyTotal2,this.form.c3,this); return chk(this.form.c3,this);">
<br>
<input name="qtyTotal2" type="text" id="qtyTotal2">
</p>
<p>
<input type="text" name="textfield3" onkeydown="calc(this.value,this.form.textfield4,this.form.checkbox3,this.form.checkbox4)" onkeyup="calc(this.value,this.form.textfield4,this.form.checkbox3,this.form.checkbox4); ">
<br>
130:
<input type="checkbox" name="checkbox3" value="180" onclick=" calc(this.form.textfield3.value,this.form.textfield4,this,this.form.checkbox4); return chk(this,this.form.checkbox4);">
<br>
1300:
<input type="checkbox" name="checkbox4" value="1800" onclick=" calc(this.form.textfield3.value,this.form.textfield4,this.form.checkbox3,this); return chk(this.form.checkbox3,this);">
<br>
<input type="text" name="textfield4">
<br>
</p>
<input type=text name="complete">
</form></body></html>