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