If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Cycle through Switch Cases

So as the title says what I'm looking to do is to be able to cycle through Switch cases. Let me go into a bit more detail.

I've created my code with a bunch of switch cases, each case representing an item. Each case is numbered, so say you type 25, click, and it comes up with item #25, it'll show a picture and information about that item. What I want to happen is to somehow be able to click a 'Previous' or 'Next' button in order to go to the next or previous item. So if I'm on item 25, I'd like to be able to click 'Next' and go to item 26.

So is there a way to do so when the information is set up in Switch cases?

It's difficult to know the exact solution without seeing at least some of your code, but you need to generate the 'Next' and 'Previous' buttons when you generate the content for 'it comes up with item #25', regardless of how your switch loop operates. Usually this requires replicating the URL in the link that was clicked on to invoke the function in the first place, but with the appropriate item number (item+1 and item-1, with checks for minimum and maximum values).

I'll link some code when I get home. What I'm basically doing for fun (might as well come out with it) is making a Pokemon Pokedex where the user types the Pokemon's name or number into the box via keyboard (or on-screen keyboard), hits the 'Search' button, and the information about the Pokemon (picture, number, type, etc) are brought up in a div that remains empty if no search has been done yet. Which means there are no URL's.

So what I'd like is, if they search for Pikachu (number 25) and his data comes up, to be able to cycle the Pokemon before or after from there. Though I don't know how I'd access the case information to make that happen, or even if it's possible.

Overall, my suggestion would be to change the code so that the function getResult() accepts a parameter named 'result' instead of determining the value of result within the function itself. As in:

Code:

function getResult(result) {
.
.
.
}

and then add the code for creating the 'Previous' and 'Next' at the end of the function based on the value of 'result'.

But even as your code stands, you can simply insert that code at the end of each case: section based on the value of 'result'. You'll just have to replicate that code for each case: section - which is why I suggested the changes above. Exactly how you create the button is up to you. More than one way to skin a cat, as they say.

Overall, my suggestion would be to change the code so that the function getResult() accepts a parameter named 'result' instead of determining the value of result within the function itself. As in:

Code:

function getResult(result) {
.
.
.
}

and then add the code for creating the 'Previous' and 'Next' at the end of the function based on the value of 'result'.

But even as your code stands, you can simply insert that code at the end of each case: section based on the value of 'result'. You'll just have to replicate that code for each case: section - which is why I suggested the changes above. Exactly how you create the button is up to you. More than one way to skin a cat, as they say.

I'm sorry I'm still pretty new at this. Can you give me an example of the code I would write/copy within each Case? I would really appreciate it.