Is there a way to dynamically assign a variable name??

T

Terry

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>
 
R

Richard Cornford

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. ...
<script language="JavaScript">
T1val=0;
T2val=0;
T3val=0;

function calc(regnum) {
<snip>

If you mean global variables then yes. Global variables are properties
of the global object and object properties can be dynamically created.

Follow the link from:-

<URL: http://www.jibbering.com/faq/#FAQ4_339 >

- for some explanation of how.

Richard.
 
T

Terry

Hi Richard,

Thank you for the link. Had to read it a few times, and study it a
bit. But that is exactly what I needed,... and works great. The link
on that page gives a great synopsis on Globabl Variables.

Terry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top