Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Call function from onclick and pass image src to it

    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 -

    Code:
    <a href="#" onclick="showImage(this.src)">
    <img src="images/peopleThb/DSC_7982-1.gif" id="i1" />
    </a>
    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.

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    onClick="showImage(document.getElementById('i1').src);"

  • #3
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that Pyth007. Dont know why I couldnt see that for myself! Simple. I was thinking far too deeply - dangerous!

  • #4
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Pyth007
    onClick="showImage(document.getElementById('i1').src);"
    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.
    Code:
    // 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
    PhotoJoe


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •