Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem adding form vairbales

Status
Not open for further replies.

knightjb1

Programmer
Feb 12, 2008
27
0
0
US
I have five text inputs
4 are user entered values
1 is to display the total
I am trying to get the total to display the total of all four boxes.

This is my javascript code...

function calcTotal(NewClaim) {
A = document.NewClaim.delegate1percent.value;
A = A * 1;
B = document.NewClaim.delegate2percent.value;
B = B * 1;
C = document.NewClaim.delegate3percent.value;
C = C * 1;
D = document.NewClaim.delegate4percent.value;
D = D * 1;
}
E = A + B + C + D;
alert(E);
document.NewClaim.total.value = E;
}

This is my Call...

<td><input type="text" name="delegate#count#percent" size="1" onChange="calcTotal(NewClaim);" /><td>

I am programming on a coldfusion page.

Anyone see anything wrong here??? The output i get when i remove the * 1 is it concatenates the values and with the *1 i get nothing.
 
sigh ... i got it i had an extra } in there wow sorry for wasting the thread space lol
 
These things are much easier to spot with nicely formatted code, as this shows:

Code:
function calcTotal(NewClaim) {
	A = document.NewClaim.delegate1percent.value;
	A = A * 1;
	B = document.NewClaim.delegate2percent.value;
	B = B * 1;
	C = document.NewClaim.delegate3percent.value;
	C = C * 1;
	D = document.NewClaim.delegate4percent.value;
	D = D * 1;
}
E  = A +  B + C + D;
alert(E);
document.NewClaim.total.value = E;
}

You can easily see the mismatched brace here.

Note: you can make your code easily scalable with a simple loop:

Code:
function calcTotal() {
	var frm = document.forms['NewClaim'].elements;
	var total = 0;
	for (var loop=1; loop<5; loop++) {
		var theVal = parseInt(frm['delegate' + loop + 'percent'].value, 10);
		total += (!isNaN(theVal)) ? theVal : 0;
	}
	alert(total);
	frm['total'].value = total;
}

Hope this helps,
Dan

P.S. I had refactored your code, but still left the mismatched brace in... I only spotted it at the last minute due to the formatting [blush]

Code:
function calcTotal() {
	var frm = document.forms['NewClaim'].elements;
	var total = 0;
	for (var loop=1; loop<5; loop++) {
		var theVal = parseInt(frm['delegate' + loop + 'percent'].value, 10);
		total += (!isNaN(theVal)) ? theVal : 0;
	}
}
alert(total);
frm['total'].value = total;
}

:)

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top