View Full Version : Call function from onclick and pass image src to it

10-21-2005, 06:16 PM
I am trying to pass the src of an image file to a function. The function is being called from a <a onclick="...."> tag.
If someone could tell me how this is possible I would be really grateful.
The code at the moment is as follows -

<a href="#" onclick="showImage(this.src)">
<img src="images/peopleThb/DSC_7982-1.gif" id="i1" />

I know its wrong at the minute, but I think I am on the right lines because if I put the onclick event handler into the img tag it works (but this is not well supported across browsers)

I want to pass the filename "images/peopleThb/DSC_7982-1.gif" to the function showImage.

10-21-2005, 06:25 PM

10-21-2005, 07:41 PM
Thanks for that Pyth007. Dont know why I couldnt see that for myself! Simple. I was thinking far too deeply - dangerous!

10-22-2005, 01:03 AM

Just for my understanding the ('i1) is the Id of an image object. You would use a <style> tag to name the <img> tag, right? But if you have not define the <img> tag already you would would not have an image object and so I don't think this would work. So could you create an image object an assign the src of this image object to a variable which can then be passed to the function, I think?

Here is a bit of javascript code from one of my js files that does this.

// Script #2 (Create an array of image objects for tumbnail of slides)

var slide = new Array ();

for(i=0; i<=19; i++)
slide[i] = new Image();
slide[i].src= "../images/Fav_Small/" + arrSlideNumbers[i] + "a.jpg";
// end of script #2