473,659 Members | 3,348 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 8989
"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
9113
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, and as you might imagine, the markup was way too bloated. So can I just change the css layers...
1
6010
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 be played with - this is not the final result so please (unless it's leading to the script failure)...
4
12567
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
4827
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
10796
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 method instead of QueryString as I don't want the carried variable to be visible for the user.
31
1191
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
2524
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. What am I doing wrong? Below is a sample HTML doc with everything in place. <!DOCTYPE HTML PUBLIC...
5
8576
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 of all these little child forms, the main control is a panel, which I then assign its parent to the...
6
1830
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 button, and using it in the name attribue of the button .But this is not working.How can I assign...
0
8851
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.