I have a number of input boxes used to display totals based on
selected items for each row in a table. There are more than a few
rows that are identical, except for the form field name. I have added
a sample of that below (there would be many more rows).
I'm wondering if there is a way to dynamically generate the variable
names (ie: T1val, T2val, etc.) in my function 'calc', based on the
argument 'regnum' that I pass it. The problem right now, is that I
have to repeat lines 9 to 15 for every row that I add, using the
specific variable names for each. If there was some way to
dynamically create and assign them using the value of 'regnum'
(similar to using 'elements'), that would be great.
I guess one way might be to create hidden fields for each of those
variables, and I could then use the 'elements' syntax to assign
values, but wondered if there is a simpler way.
Any help is greatly appreciated.
Terry.
Sample code:
<HTML><HEAD>
<script language="JavaScript">
T1val=0;
T2val=0;
T3val=0;
function calc(regnum) {
with (document.form1) {
if (regnum==1) {
T1val=0;
for (i=1;i<3;i++) {
if (elements['q'+regnum+i].checked)
T1val=T1val+eval(elements['q'+regnum+i].value);
}
elements['T'+regnum].value=T1val;
}
if (regnum==2) {
T2val=0;
for (i=1;i<3;i++) {
if (elements['q'+regnum+i].checked)
T2val=T2val+eval(elements['q'+regnum+i].value);
}
elements['T'+regnum].value=T2val;
}
Tval=T1val+T2val;
T.value=Tval;
}
}
</script>
</HEAD>
<BODY>
<form method="post" name="form1">
<table>
<tr>
<td>100<input type="checkbox" value="100" name="q11"
onclick=calc(1)></td>
<td>200<input type="checkbox" value="200" name="q12"
onclick=calc(1)></td>
<td> subTotal1</td>
<td><input name="T1" ></td>
</tr>
<tr>
<td>300<input type="checkbox" value="300" name="q21"
onclick=calc(2)></td>
<td>400<input type="checkbox" value="400" name="q22"
onclick=calc(2)></td>
<td> subTotal2</td>
<td><input name="T2" ></td>
</tr>
<tr>
<td>Total</td>
<td><input name="T" size="23"></td>
</tr>
</table>
</form>
</BODY>
</HTML>