[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:

http://www.doxdesk.com/personal/posts/wd/20011010-eval.html

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:

<script>
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
purpose:

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

Then the function:

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

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

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

  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
variables.

HTH

Tom

=====
>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
http://www.pixelmech.com/
var me = tom.pixelmech.webDeveloper();

http://www.maccaws.com/
[Making A Commercial Case for Adopting Web Standards]

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/



More information about the thelist mailing list