Accessing a variable through a form name

C

C Gillespie

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.option[1].text returns 'B1'

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

document.form.option[1].text[1] returns 'sink'

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

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

but this gets a bit messy.

Thanks

Colin
 
C

Cenekemoi

Bonjour à C Gillespie said:
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.option[1].text returns 'B1'

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

document.form.option[1].text[1] returns 'sink'

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

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

but this gets a bit messy.

Thanks

Colin

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

alert(toto) --> sink
 
M

Michael Winter

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

document.form.option[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.
 

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

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top