View Full Version : finding out and changing image src

01-15-2003, 04:10 PM
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,

01-15-2003, 04:59 PM
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.