PDA

View Full Version : How to use ID to reference a variable?



rlincolnh
11-28-2004, 05:33 AM
I have many areas defined in an image, and a single onmouseover handler. I need to have a number of different data items associated with each area and be able to access those data from within the handler. So at the beginning we define something like this:

<AREA id="defaultmode" onMouseover="maplinkover(event)" ...>
<AREA id="mode1" onMouseover="maplinkover(event)" ...>

var defaultmode=[value1, value2, value3, ...];
var mode1=[value5, value6, value7, ...];

Then in the onmouseover handler I retrieve the id of the area that triggered the event (say it was "mode1") and then want to do things like:

instrument_set=mode1[0]; power_level=mode1[1]; etc.

But of course the id is just a string 'm' 'o' 'd' 'e' '1' and has no relationship to the variable mode1.

My question: How do I make that association? I realize that I could spin around in a loop comparing the id to an array of id strings, hence forming an index that I could use to access another array (or array of arrays) to get the data that I want, but that seems extremely messy, inelegant, and a nightmare-in-waiting for when I need to add, alter, or delete areas and values.

I'm sure there must be a better way - help, anyone, please?

rlincolnh
11-28-2004, 07:59 AM
Hey, if anyone has any really smooth ideas on this, I'd really like to hear them.

But I've just finished a bit more reading on arrays, and it turns out that *all* arrays in javascript are associative, even if you don't normally use or think of them that way. That makes my job about as easy as I think it realistically can be. A few quick'n'dirty tests worked exactly as I dared hope, so I'm off and into it!