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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2003
    Location
    Berlin
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    finding out and changing image src

    Hi, I have a number of images and want the user to browse through them using a 'back' and a 'next' button.
    The image names are all the same, only the number in the end is incremental, e.g
    pic_01.gif, pic_02.gif etc.
    In order to change them, how do i find out and reuse the number of the current picture?
    Thank you very much,
    C

  • #2
    Regular Coder
    Join Date
    Dec 2002
    Location
    the windy city
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var pic_total = 20 // totlal number of your pictures
    var current_pic = 1 //the first picture when the page loads
    
    function show_pic(num) {
    current_pic = current_pic + num
    if (current_pic > pic_total) {
     current_pic = 1
    }
    if (current_pic < 1) {
     current_pic = pic_total
    }
    
    if (current_pic < 10) {
     document.getElementById('my_pic').src = "images/pic_0" + current_pic + ".gif"
    }
    else {
     document.getElementById('my_pic').src = "images/pic_" + current_pic + ".gif"
    }   
    }
    your "back" button should call "show_pic(-1)" and your "next" button should call "show_pic(1)"
    this also assumes that you have a total of 20 images (you can change the number to however many you have, and also that your image's id/name is "my_pic"
    the code is not tested, but should work.

    HTH


  •  

    Posting Permissions

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