[thelist] javascript "won't" subtract

Tom Dell'Aringa pixelmech at yahoo.com
Sat Mar 8 11:49:00 CST 2003

--- Emma Jane Hogbin <emmajane at xtrinsic.com> wrote:

> The "start" button only works if I click it first, not second. And
> and the previous button doesn't ever work. The next button only
> works if I click it first and (or right after the first click of
> the start button). And then works sequentially up. (But I can't
> use any of the other buttons in the sequence.) The previous button
> never works.
> http://queenwest.ca/lomo/emmajane/popnine1/

Emma, a few things. #1 - almost without exception you should never
need to use the eval() method. For more on this read:


Secondly, when I have more than two if statements I personally like
to use the switch control structure, its much cleaner.

Now, all you really need to do is kick up and down the number of your
slide. It seems to me that the number slide the person should be on
in the page is the same as your photo, this makes things easy. All
you need is a counter, and then to call the number of the current
photo. So your 'home' page is 0. When they click next you add one,
previous, subtract one.

So first when the page loads, set a variable to 0:

var slideCount = 0;

Make sure you do this OUTSIDE your function. Then the function should
be called by your buttons of course, and have each button send its

<button name="next onclick="showImg('next');"> etc.

Then the function:

function showImg(whatToDo)
    case "start":
    slideCount = 1;  //just go to 1

    case "next":
    slideCount += 1;  //adding one to it

    case "previous":
    slideCount -= 1;  //subtracting one from it

  var photo = document.getElementById('+slideCount+').style;
  photo.display = "block";
  photo.visibility = "visible";

This is untested but should work. I'm not sure why you are using
visibility, I didn't take much of a look into the page so I left it
there. Also, not sure where you are hiding the previous one, but you
could handle that here too.

When the page loads, your slideCount variable will reset itself for
the next user. You could save the value with a cookie or server side



var me = tom.pixelmech.webDeveloper();

[Making A Commercial Case for Adopting Web Standards]

Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more

More information about the thelist mailing list