...

View Full Version : For Loop



JoeP
01-17-2005, 10:57 PM
I have a form with 36 fileds. For demo Fields are named Field1...Field36
A total Field named SumTotal

Would someone please show me proper syntax to run a FOR loop to add the fields on a call to a function called Add_Fields() and get the variable sumTotal.

e.g.: Something like:

<SCRIPT>
function Add_Fields(){
var sumTotal=0
for(i = ; i <= 36; i++)
{sumTotal=document.forms.Data_Entry.Field+i.value}
alert('Your Total'+sumTotal)
document.forms.Data_Entry.value=sumTotal
}
</SCRIPT>

:o

hemebond
01-18-2005, 12:48 AM
<script type="text/javascript">
function Add_Fields()
{
var sum = 0;
for(i = 1; i <= 36; i++)
{
sumTotal += parseInt(document.forms.Data_Entry.Field+i.value);
}
alert('Your Total'+sumTotal);
document.forms.Data_Entry.total.value = sumTotal;
}
</script>

glenngv
01-18-2005, 01:32 AM
sumTotal += parseInt(document.forms.Data_Entry.Field+i.value);

That should be:

sumTotal += parseInt(document.forms.Data_Entry.elements["Field"+i].value);

or:

sumTotal += parseInt(document.forms.Data_Entry["Field"+i].value);


See the square bracket notation in my sig.

JoeP
01-18-2005, 02:05 AM
Thanks to the forum for fast response....

hemebond ~ Thank You!

glenngv ~ Salamat sa inyo


:thumbsup:

glenngv
01-18-2005, 02:26 AM
glenngv ~ Salamat sa inyo
:thumbsup:
walang anuman. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum