Pass value to array as key

U

UKuser

Hi,

I have a function that picks up iPd as a value and I then want it to
pass it to return the specific image:

na = new Image();na.src = "world/na_over.gif";
sa = new Image();sa.src = "world/sa_over.gif";
eu = new Image();eu.src = "world/eu_over.gif";

if (action=='over'){
document.getElementById(iPd).src = iPd.src; [Where iPd is na or
sa]
}

I have tried using an array:

countries = new Array();
countries['eu'] = eu.src;

if (action=='over'){
document.getElementById(iPd).src = countries[iPd];
}

But this won't work either. I'm sure its something simple - just
missing the obvious of how to pass the value to either countries[] or
reference it directly. (in PHP I could use variable variables but
don't believe this can be done in JS)

Many thanks

A
 
U

UKuser

Hi,

I have a function that picks up iPd as a value and I then want it to
pass it to return the specific image:

na = new Image();na.src = "world/na_over.gif";
sa = new Image();sa.src = "world/sa_over.gif";
eu = new Image();eu.src = "world/eu_over.gif";

if (action=='over'){
document.getElementById(iPd).src = iPd.src; [Where iPd is na or
sa]
}

I have tried using an array:

countries = new Array();
countries['eu'] = eu.src;

if (action=='over'){
document.getElementById(iPd).src = countries[iPd];
}

But this won't work either. I'm sure its something simple - just
missing the obvious of how to pass the value to either countries[] or
reference it directly. (in PHP I could use variable variables but
don't believe this can be done in JS)

Many thanks

A

Fixed it - was passing in upper case variables but countries[''+iPd
+''] did it.
 
T

Thomas 'PointedEars' Lahn

UKuser said:
[...]
if (action=='over'){
document.getElementById(iPd).src = countries[iPd];
}
[...]

Fixed it - was passing in upper case variables but countries[''+iPd
+''] did it.

Your explanation does not make sense. foo[bar] is semantical identical to
foo['' + bar + ''].


PointedEars
 

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,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top