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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2002
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What is the image number?

    Hello,
    I have some images on the screen, displayed from the array im[ ].
    When I click on an image, I would like that the onclick= function stores the image number in a cookie named 'showpic'. What is the way to get the number of the image I clicked on?

    <script language="JavaScript">
    for (var i = 1; i <= maxim; i++)
    {
    document.write('<img src="'+im[i]+'" vspace="5" hspace="5" width="100" height="75" border="1" alt="" onclick="set(\'showpic\',i);">');
    }
    </script>

    Thanks in advance
    Richard

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When the user clicks the image, you could get it like this:

    Code:
    function getImageNum(n) {
    var src = n.getAttribute('src');
    
    for(var i = 1; i <= maxim; i++) {
    
    if(im[i] == src) {
    
    return i;
    
    }
    
    }
    
    }
    Then, onclick you could set the cookie:

    Code:
    <img ... onclick="setCookie('imgnum', getImageNum(this));" />
    Hope that helps!

    Happy coding!

  • #3
    New Coder
    Join Date
    Sep 2002
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank You so much nolachrymose, it works!
    In fact, the variable src in your answer contained already what I needed.
    May God bless you.
    Richard


  •  

    Posting Permissions

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