473,670 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing a variable through a form name

Dear All,

I have 2 arrays

var A1 = new Array();
A1[ 0 ] ="Y2";

var B1 = new Array();
B1[ 0 ] ="Y1";
B1[ 1 ] ="sink";

I also have a drop down menu with the names of the arrays. Say

document.form.o ption[1].text returns 'B1'

Can I transform the string 'B1' into the array B1, so that

document.form.o ption[1].text[1] returns 'sink'

Currently I'm doing this with conditionals, e.g.

if(document.for m.option[1].text =="B1){do stuff}

but this gets a bit messy.

Thanks

Colin


Jul 23 '05 #1
2 2389
Bonjour à C Gillespie <cs******@hotma il.com> qui nous a écrit :
Dear All,

I have 2 arrays

var A1 = new Array();
A1[ 0 ] ="Y2";

var B1 = new Array();
B1[ 0 ] ="Y1";
B1[ 1 ] ="sink";

I also have a drop down menu with the names of the arrays. Say

document.form.o ption[1].text returns 'B1'

Can I transform the string 'B1' into the array B1, so that

document.form.o ption[1].text[1] returns 'sink'

Currently I'm doing this with conditionals, e.g.

if(document.for m.option[1].text =="B1){do stuff}

but this gets a bit messy.

Thanks

Colin


arrayName = 'B1';
toto = window[arrayName][1];

alert(toto) --> sink

--
Cordialement, Thierry ;-)

Jul 23 '05 #2
On Mon, 16 Aug 2004 15:20:05 +0100, C Gillespie <cs******@hotma il.com>
wrote:

[snip]
Can I transform the string 'B1' into the array B1, so that

document.form.o ption[1].text[1] returns 'sink'


I apologise if the following explanation sounds confusing. Hopefully, the
code will help.

Whenever you declare a variable in global scope, that variable becomes a
property of the global object.

When you need to access a property and the name of that property is stored
in a variable, the proper way to access it is to use the bracket property
accessors. This is often used with form controls.

We can combine these two facts, resulting in a method to access a global
variable using another variable.

/* In global scope. */
var global = this;

/* This notation is more compact. */
var A1 = ['Y2'],
B1 = ['Y1', 'sink'];

/* In your function. */
/* Get a reference to a form. */
var f = document.forms['formName'];
/* Get a reference to the SELECT element. */
var s = f.elements['selectElement'];

/* Get a reference to the array. */
var a = global[s.options[s.selectedIndex].value];

Assuming that the value of the currently selected option is 'B1', a[0]
will yield 'Y1' and a[1] will yield 'sink'.

If the arrays are actually members of an object, substitute 'global' in
the last line for the name of (reference to) the object.

If the arrays are declared inside a function, we must make the the
properties of an accessible object[1]. To do that, make the a member of
the function itself:

function myFunction() {
/* Body of function. */
}
myFunction.A1 = ['Y2'];
myFunction.B1 = ['Y1', 'sink'];

[snip]

Hope that helps,
Mike
[1] Local variables within a function are assigned to what's called, the
activation object. Unfortunately, this object cannot be directly accessed.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2736
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is called "form1", and I have selects called "PORTA", "PORTB" ... etc...
6
1858
by: J Mox | last post by:
I have a function that changes which radio button is selected. I need to pass the form name to the function but not the field name and am doing so like: changeRadio(this.form); The function: changeRadio(formname){ // I have skipped how the variable fieldname is created all that is important is that it needs to be
3
2790
by: Vivek Sharma | last post by:
Hi, I have created a dropdownlist as a web user control. I am using its multiple instances on the webpage. How do I access the selectedValue of each instance? All the instances have different IDs. Thanks Vivek
1
3120
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
19
10536
by: k.karthikit | last post by:
Hello all, In some hidden variable (<input type="hidden" name="hiddenId" value="test" /> ,i stored some value.I accessed the value "test" using var id = document.getElementById( 'hiddenId' ); It is working fine in IE. But in Mozilla Firefox , null value is returned.Is there any way to use hidden variables compatible to browsers......?
5
2361
by: nomail.thanks | last post by:
I apologize for the question in advance. I have lots of experience with lots of other languages, but now I'm trying to accomplish something with VB.NET Express. My experience with VB.NET express is less than a week. I've gone through what appear to be the relevant tutorials that came with the product, consulted the two reference books I have, explored the source code that is being generated by my project, and played around trying...
4
3622
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if possible.. for example..i hav a menu in the parent form named "Administrator" whic has an item "mnuLogIn"..now when i click on login..another child form named "frmLogIn" is displayed..what i want to happen is this: when login form(frmLogIn) is...
8
2178
by: colmkav | last post by:
Hi, could someone tell me how I can check whether a database is open by name eg something like db("mydbname")
9
1910
by: JohnR | last post by:
I have the name of a control in a string variable and I want to change one of the controls properties. Right now I recursively scan all the controls on the form until I get one whose name matches the name in my string variable, then I know I have the correct control and can proceed to change the property. However all that searching seems like overkill. Does anyone know of a way to directly access the control if it's name is in a string...
0
8469
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8592
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
7419
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
6213
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
4211
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...
0
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2800
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
2
2042
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.