JavaScript - variable/object dichotomy

I’m trying to write a better and more efficient image rollover routine at the moment. I’m having a problem with using a variable name to identify an image, and then using that variable to represent the object. Does anyone know how to do this? Here’s some sample code:


<SCRIPT>
var im = new Array();
var img = new Array();
var startToggle;
// preloads images
function doPreload()
{
img[0] ="./images/page_off.gif";
img[1] ="./images/page_on.gif";

for (var i = 0; i < img.length; i++)
	{
	im* = new Image();
	im*.src = img*;
	}
}
//toggles image
function toggle(num)
{
num.src = im[num].src;
}

</script>
[SNIP]
<A HREF="./index.html" OnMouseOver="toggle(1)"><img src="images/page_off.gif" border="0" alt="" name="1">


Bump!

Most unpopular GQ ever? :smiley:

To make myself clearer, I’ll transcribe the salient point here:

num.src = im[num].src;

I have attempted to use the variable ‘num’ (value is “1”) as the name of the image (also “1”) and as a numerical variable, value = 1. Needless to say, is doesn’t work. Any ideas?

Your problem is your script is having problems accessing the right object in the document. IIRC you can either name the objects and access them that way, or find out the index in the appropiate array and use that.

My brain is mushy right now. I may dig up some info tomorrow.