# [SOLVED] Javascript newbie &lt;&lt;&lt;&lt;



## tacmed1 (Jul 14, 2008)

Hey there. I am doing a website for a client and they're looking for a simple slideshow... the clickable kind. I'm having trouble with the Javascript. All I need is the pictures to rotate within one cell on one page when clicked upon. Anyone have a script lying around they can lend me to reverse engineer or a website that goes over it (besides javascriptkit, there's some compatibility issues there some how)?

Thanks

Jim


----------



## jamiemac2005 (Jul 5, 2007)

*Re: Javascript newbie <<<<*

Hey, it would be useful if you could post your code (e.g. the code which you've tried to write)... anyway, i can mock up a basic script:


```
//set up an array to hold the url/relative url of the images(src)
var images = new Array();
slideshowImages[0] = "image1.jpg";
slideshowImages[1] = "image2.jpg";
slideshowImages[2] = "image3.jpg";

//a counter to hold the current image number(index)
var currentImage = 0;

//a function to swap the image
function swap(id, image){
    //set the src of the image as the new image url
    document.getElementById(id).src = image;
}

//a function to rotate/cycle between all images
function cycleImages(id){
    //increment the current image's index
    currentImage++;
    //if it's the maximum number of images then make it zero
    //note: the index starts at zero so this is correct (ask for more help if necessary)
    if(currentImage == slideshowImages.length){
        currentImage = 0;
    }
    swap(id, slideshowImages[currentImage]);
}

//Implementation:
//you need the id of the image (<img id="blah"... for the sakes of this example)
//to cycle once run:
cycleImages("blah");

//to cycle constantly run:
var slideshowInterval = setInterval("cycleImages('blah');", 1000);
//1000 being 1000 milliseconds (one second)
```
This would work(havent checked it so if it doesn't then post to tell me) as a forward cycling slideshow, it can be adapted easily to cycle backwards aswell as forwards...
Then of course if you wan't it to just be clickable then set up an item with the onClick event handler of "cycleImages('blah');"...

If you need this extending or if you need more help then post back...

Cheers,
Jamey


----------



## jamiemac2005 (Jul 5, 2007)

*Re: Javascript newbie <<<<*

p.s. if you do have code with a more specific nature (e.g. you have already set up the webpage and you need to just make things work).. then feel free to post it and we can take a look at it.

Cheers,
Jamey


----------



## tacmed1 (Jul 14, 2008)

*Re: Javascript newbie <<<<*

Thanks Jamey. After getting to know it, it worked! Let's knock on wood.


----------



## jamiemac2005 (Jul 5, 2007)

*Re: Javascript newbie <<<<*

haha kk, glad it worked.

Cheers,
Jamey


----------

