Function count back and forth on event

I have two events: next and previous.
I need a counter from 0 - 5, +1 when next is triggered, -1 when previous is trigger.

Can you show us the code you have so far, and maybe we can point you in the right direction.

2 Likes

The event code for each of those would be:

function nextClickHandler(evt) {
  // ...
}
function previousClickHandler(evt) {
  // ...
}

The counter would need to be accessible by the click handlers, so declaring a counter variable in the same scope as those click handlers, is a good idea.

var counter = 0;
function nextClickHandler(evt) {
  // ...

Now the click handlers can access that counter variable, and modify it.

var counter = 0;
function nextClickHandler(evt) {
  if (counter < 5) {
    counter += 1;
  }
}
function previousClickHandler(evt) {
  if (counter > 0) {
    counter -= 1;
  }
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.