Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Javascript difficulty, swapping images?

I am using this very simple script (just learning; again) in a button called "next". Now I want to create a button called "previous" and adapt the script into a function "swapBack", but all my guesses have failed. I would really like to keep it supersimple.

function swapImage()

{

document.slide.src = path[i];

if(i < path.length - 1) i++; else i = 0;

onClick("swapImage();");

}

Update:

Thanks Silent, But my levels of incompetence are beyond your ken. I have tried every permutation along the lines of

if(i > path.length + 1) i+-; else i = 3;

but i get nowhere with my swapBack. Would you mind being more explicit?

1 Answer

Relevance
  • Silent
    Lv 7
    1 decade ago
    Favorite Answer

    Look at what you're doing right now. You're adding one to the global variable i, so that the next call to swapImage() gets the next image. If that would make i land outside the array, you set it to 0 so it goes back to the beginning.

    What you need to do is reverse this process. Instead of adding one, subtract one. If that would make i less than 0, set it to the highest value in the array (path.length - 1) so that it goes back to the end.

    Edit:

    The way to figure out something like this is not to try random permutations of things, but to examine carefully what you're doing and how it works. These are not just random symbols; they all have specific functions, and if you take the time to read the documentation and learn what those functions are, you'll find this all a lot easier.

    That having been said, something like this will do what you want:

    if (i >= 0) i--; else i = path.length - 1;

Still have questions? Get your answers by asking now.