Issue with value not being passed down correctly.

Am still working on learning pagination. I am trying to use less repeated code, so turned my button click into its own function. When I click a number I can get it to go to the correct page, but when I use the previous button, it changed the current page to 0 no matter what the page is. The initial value is set at 1. I understand this means the function call is using the initial value. https://codepen.io/bsups/pen/xxypaWE
Brandon
CodePen
xxypaWE
...
5 Replies
Chris Bolson
Chris Bolson2y ago
This is because you are passing the default pageNumber values to the buttonClick function when you initalize the script. Therefore, when you click on any of the buttons, it already has the value. This is fine for the numbers but for the "previous" button you need to pass the value at that moment.
Errtu
Errtu2y ago
I am confused on how to do that Seems like my whole current function set up should be changed to allow that I do see what you mean, when i call prevButton at the bottom the page value is 1
Chris Bolson
Chris Bolson2y ago
There are many ways to do this and to be honest I would probably go about it quite differently. However, to not refactor too much of your code, you could try passing something other than a number as the value in the back button, eg. "<". Then, in your buttonClick function you could detect this value and calculate the current page by subtracting 1 at this point (remember to check that it isn't <1). I have forked your codepen and made a few adjustments. I didn't want to refactor too much code but I did remove the parameter "pageNumber" that you were passing to the buttonClick function as it wasm't needed. https://codepen.io/cbolson/pen/eYPyLQp Take a look and see if it works for you.
Errtu
Errtu2y ago
I see, figured I would need need to use some if's depending on what button i choose makes sense, thank you. I will just rework it all
Errtu
Errtu2y ago
Thank you @Chris for all your help. I was able to finish understanding it. Ill come back to it and figure out how to properly display certain page number buttons pending on what page the user is currently viewing. https://codepen.io/bsups/pen/VwEQQoq Thanks again.
Brandon
CodePen
VwEQQoq
...
Want results from more Discord servers?
Add your server