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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I do this in a loop?

Status
Not open for further replies.

calista

Programmer
Jan 24, 2001
545
US
Here's what I'm trying to do. I want to present a series of textboxes for the user to enter numbers. I want the total to update dynamically as each number is entered. What I have here does that, but it's rather messy. I know how to display the textboxes using a loop, but I can't get the script to work with it. Any suggestions?
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<HTML>
<HEAD>
	<TITLE>Untitled</TITLE>
	<SCRIPT>
<!--
function calculate(which)
    {
    	document.MyForm.TotalRegHours.value = 
			  (document.MyForm.TotalRegHours.value - 0) 
			+ (document.MyForm.RegHoursOne.value - 0)
			+ (document.MyForm.RegHoursTwo.value - 0)
			+ (document.MyForm.RegHoursThree.value - 0)
			+ (document.MyForm.RegHoursFour.value - 0)
			+ (document.MyForm.RegHoursFive.value - 0);
    	return; 
    }
//-->
</SCRIPT>
</HEAD>

<BODY>

<CFPARAM NAME=&quot;TotalRegHours&quot; DEFAULT=&quot;0&quot;>
<CFSET RegHours = &quot;0&quot;>

<CFOUTPUT>
<FORM ACTION=&quot;#CGI.SCRIPT_NAME#&quot; METHOD=&quot;post&quot; NAME=&quot;MyForm&quot; ID=&quot;MyForm&quot;>
	<INPUT TYPE=&quot;text&quot; NAME=&quot;RegHoursOne&quot; CLASS=&quot;WhiteTextBox&quot; VALUE=&quot;#RegHours#&quot; SIZE=&quot;13&quot; ONKEYUP=&quot;calculate(this)&quot;><BR>
	<INPUT TYPE=&quot;text&quot; NAME=&quot;RegHoursTwo&quot; CLASS=&quot;WhiteTextBox&quot; VALUE=&quot;#RegHours#&quot; SIZE=&quot;13&quot; ONKEYUP=&quot;calculate(this)&quot;><BR>
	<INPUT TYPE=&quot;text&quot; NAME=&quot;RegHoursThree&quot; CLASS=&quot;WhiteTextBox&quot; VALUE=&quot;#RegHours#&quot; SIZE=&quot;13&quot; ONKEYUP=&quot;calculate(this)&quot;><BR>
	<INPUT TYPE=&quot;text&quot; NAME=&quot;RegHoursFour&quot; CLASS=&quot;WhiteTextBox&quot; VALUE=&quot;#RegHours#&quot; SIZE=&quot;13&quot; ONKEYUP=&quot;calculate(this)&quot;><BR>
	<INPUT TYPE=&quot;text&quot; NAME=&quot;RegHoursFive&quot; CLASS=&quot;WhiteTextBox&quot; VALUE=&quot;#RegHours#&quot; SIZE=&quot;13&quot; ONKEYUP=&quot;calculate(this)&quot;><BR><BR>
	<INPUT TYPE=&quot;text&quot; NAME=&quot;TotalRegHours&quot; CLASS=&quot;BlueTextBox&quot; VALUE=&quot;#TotalRegHours#&quot; READONLY SIZE=&quot;13&quot;>
</FORM>
</CFOUTPUT>
<BR><BR>
</BODY>
</HTML>
Thanks! Calista :-X
Jedi Knight,
Champion of the Force
 
try this:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<SCRIPT>
<!--
function calculate(which)
{ document.MyForm.TotalRegHours.value = parseInt(document.MyForm.TotalRegHours.value) + parseInt(which.value);
}
//-->
</SCRIPT>
</HEAD>

<BODY>

<CFPARAM NAME=&quot;TotalRegHours&quot; DEFAULT=&quot;0&quot;>
<CFSET RegHours = &quot;0&quot;>
<CFOUTPUT>
<FORM ACTION=&quot;#CGI.SCRIPT_NAME#&quot; METHOD=&quot;post&quot; NAME=&quot;MyForm&quot; ID=&quot;MyForm&quot;>
<cfloop from=&quot;1&quot; to=&quot;5&quot; index=&quot;count&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;RegHours_#count#&quot; CLASS=&quot;WhiteTextBox&quot; VALUE=&quot;#RegHours#&quot; SIZE=&quot;13&quot; ONBLUR=&quot;calculate(this)&quot;><BR>
</cfloop>
<INPUT TYPE=&quot;text&quot; NAME=&quot;TotalRegHours&quot; CLASS=&quot;BlueTextBox&quot; VALUE=&quot;#TotalRegHours#&quot; READONLY SIZE=&quot;13&quot;>
</FORM>
</CFOUTPUT>
<BR><BR>
</BODY>
</HTML>

webron
 
I've tried this, and it works except for one thing. It doubles the amount entered before adding it to the total. I'll play with it some more, but any suggestions are welcome. Calista :-X
Jedi Knight,
Champion of the Force
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top