PDA

View Full Version : Calculating dynamic form fields



torrentgirl
Jun 23rd, 2006, 06:57 PM
:eek: Can anyone help me with calculating dynamically created form fields?


I am creating form fields with a loop/index. So i have field1, field2, feild3, with the 1,2,3 added dynamically.

i need to total these dynamic fields. It can be anywhere from 1 -50

Please help.

glenngv
Jun 23rd, 2006, 07:28 PM
function calculate(){
var field, frm = document.theFormName;
var total = 0, index = 1;
while (field = frm.elements["field" + (index++)]) {
total += Number(field.value);
}
alert(total);
}

vwphillips
Jun 23rd, 2006, 07:59 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>
<script type="text/javascript">

function zxcCalculate(zxcid){
var zxctable=document.getElementById(zxcid);
var zxcrows=zxctable.getElementsByTagName('TR');
var zxcstary=[];
var zxcgtotal=0;
var zxcgtotalip=null;
for (var zxc0=0;zxc0<zxcrows.length;zxc0++){
var zxcstotal=0;
var zxcstotalip=null;
var zxcips=zxcrows[zxc0].getElementsByTagName('INPUT');
for (var zxc1=0;zxc1<zxcips.length;zxc1++){
if (zxcips[zxc1].className.match('ip')){
if (zxcips[zxc1].value.length>0&&!isNaN(zxcips[zxc1].value)){
zxcstotal+=zxcips[zxc1].value*1;
}
}
if (zxcips[zxc1].className.match('st')){
zxcstary[zxc0]=zxcips[zxc1];
}
if (zxcips[zxc1].className.match('gt')){
zxcgtotalip=zxcips[zxc1];
}
}
if (zxcstary[zxc0]){ zxcstary[zxc0].value=zxcstotal; zxcgtotal+=zxcstotal; }
}
if (zxcgtotalip){ zxcgtotalip.value=zxcgtotal; }
}

</script>
<body>
<table id="T1"border="1">
<tr>
<td width="100" ><input class="ip" type="text" size="4" ></td>
<td width="100" ><input class="ip" type="text" size="4" ></td>
<td width="100" ><input class="ip" type="text" size="4" ></td>
<td width="100" ><input class="st" type="text" size="4" ></td>
</tr>
<tr>
<td width="100" ><input class="ip" type="text" size="4" ></td>
<td width="100" ><input class="ip" type="text" size="4" ></td>
<td width="100" ><input class="ip" type="text" size="4" ></td>
<td width="100" ><input class="st" type="text" size="4" ></td>
</tr>
<tr>
<td width="100" ></td>
<td width="100" ></td>
<td width="100" ><input class="gt" type="text" size="4" ></td>
<td width="100" ><input type="button" value="Calculate" onclick="zxcCalculate('T1');" ></td>
</tr>
</table>
</body>

</html>

torrentgirl
Jun 23rd, 2006, 08:38 PM
for this code

function calculate(){
var field, frm = document.theFormName;
var total = 0, index = 1;
while (field = frm.elements["field" + (index++)]) {
total += Number(field.value);
}
alert(total);
}


does it matter that all of my fields in the form should not be included in this calulation?

I just want certain form fields calculated.

glenngv
Jun 23rd, 2006, 10:20 PM
for this code

function calculate(){
var field, frm = document.theFormName;
var total = 0, index = 1;
while (field = frm.elements["field" + (index++)]) {
total += Number(field.value);
}
alert(total);
}


does it matter that all of my fields in the form should not be included in this calulation?

I just want certain form fields calculated.
It will only calcute the fields named field1, field2, ...fieldn which you said your fields have that name pattern.

If you have other name pattern, just change the red part below

while (field = frm.elements["field" + (index++)])
with the one you're using.

torrentgirl
Jun 24th, 2006, 06:29 AM
Vic do you have a version of your code that does the same thing using onBlur?

torrentgirl
Jun 24th, 2006, 09:13 PM
Glenn I implemented your code and it is not working for me.

My form field is annualWrittenPremium#j# with J being the index.


Here is my script:

function calculate(){
var field, frm = document.frmAppForm;
var total = 0, index = 1;
while (field = frm.elements["annualWrittenPremium" + (index++)]) {
total += Number(annualWrittenPremium.value);
}
alert(total);
}


And here is my text field:

<input name="<cfoutput>annualWrittenPremium#j#</cfoutput>" type="text" onBlur="calculate();" value="<cfoutput>#Evaluate("AnnualWrittenPremium" & j)#</cfoutput>" size="12" maxlength="12" />


What am I doing wrong :confused:

glenngv
Jul 4th, 2006, 10:20 AM
function calculate(){
var field, frm = document.frmAppForm;
var total = 0, index = 1;
while (field = frm.elements["annualWrittenPremium" + (index++)]) {
total += Number(field.value);
}
alert(total);
}
And if the index starts at 0 not 1, you have to change the initial value of index to 0.