473,585 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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="JavaS cript">
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+eva l(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+eva l(elements['q'+regnum+i].value);
}
elements['T'+regnum].value=T2val;
}
Tval=T1val+T2va l;
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
4 8987
"Terry" <sa********@sha w.ca> wrote in message
news:91******** *************** *********@4ax.c om...
<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="JavaS cript">
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
In article <bm************ *******@news.de mon.co.uk>, "Richard Cornford"
<Ri*****@litote s.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
"HikksNotAtHome " <hi************ @aol.com> wrote in message
news:20******** *************** ****@mb-m13.aol.com...
In article <bm************ *******@news.de mon.co.uk>, "Richard Cornford"
<Ri*****@litot es.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
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*****@litote s.demon.co.uk> wrote:
"HikksNotAtHom e" <hi************ @aol.com> wrote in message
news:20******* *************** *****@mb-m13.aol.com...
In article <bm************ *******@news.de mon.co.uk>, "Richard Cornford"
<Ri*****@lito tes.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
9105
by: TheKeith | last post by:
I don't know much about javascript, so take it easy on me. Is there a way to dynamically change a CSS layers dimensions on the fly. Here is what I'm doing. I have a bunch of thumbnails that when clicked on, will open up the full view in a CSS layer, as opposed to a pop-up window let's say. I had this before, but with one layer for each image,...
1
6008
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that required fields in a form have values. I'm writing the values to the client using document.write only so that I can confirm that the values are there to...
4
12563
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; document.getElementById('myelement') = new Function("myfnc(param1,param2,param3)");
8
4821
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
3
10790
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would then like that, when clicking the LinkButton, the user can be navigated to another page, carrying a variable. I would like to use server.transfer...
31
1184
by: jcr | last post by:
Hi: I am having trouble dynamically setting a variable. The following code does not work. Any ideas would greatly be appreciated. Thanks, Jim Dim astr As String Dim bstr As String Dim myval As Double
7
2522
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written several methods that are working fine. The problem is if I want to dynamically assign an event handler to the object, the event handler is not called....
5
8571
by: marfi95 | last post by:
I have a form that has a left and right panel. In the left panel is a treeview. The right panel I want to change dynamically based on the type of node selected. What I'm doing is loading the treeview nodes through an XML file. As part of each node in the XML, I'm using an attribute that indicates the name of a sub form to load. As part...
6
1820
by: likhin M | last post by:
Hi everyone, I am developing a webpage using only javascript.And I am dynamically creating buttons inside javascript as follows document.write('<button name="" onclick="">button1</button>'); For each button I am creating , I have to use different names.I have tried by using a javascript string variable to hold the name of the...
0
7904
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8195
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7947
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5707
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3831
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3856
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2340
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1444
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.