By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,209 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,209 IT Pros & Developers. It's quick & easy.

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

P: n/a
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>

Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Terry" <sa********@shaw.ca> wrote in message
news:91********************************@4ax.com...
<snip>
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. ... <snip> <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.
Jul 20 '05 #2

P: n/a
In article <bm*******************@news.demon.co.uk>, "Richard Cornford"
<Ri*****@litotes.demon.co.uk> writes:
Follow the link from:-

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

- for some explanation of how.


We are up to 339 now? <ducking>
--
Randy
Jul 20 '05 #3

P: n/a
"HikksNotAtHome" <hi************@aol.com> wrote in message
news:20***************************@mb-m13.aol.com...
In article <bm*******************@news.demon.co.uk>, "Richard Cornford"
<Ri*****@litotes.demon.co.uk> writes:
Follow the link from:-

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

- for some explanation of how.


We are up to 339 now? <ducking>


Oops! :) I intended:-

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

(I did a bit much cutting and pasting and not enough deleting).

Richard.
Jul 20 '05 #4

P: n/a
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.
On Tue, 14 Oct 2003 02:44:17 +0100, "Richard Cornford"
<Ri*****@litotes.demon.co.uk> wrote:
"HikksNotAtHome" <hi************@aol.com> wrote in message
news:20***************************@mb-m13.aol.com...
In article <bm*******************@news.demon.co.uk>, "Richard Cornford"
<Ri*****@litotes.demon.co.uk> writes:
Follow the link from:-

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

- for some explanation of how.


We are up to 339 now? <ducking>


Oops! :) I intended:-

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

(I did a bit much cutting and pasting and not enough deleting).

Richard.


Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.