+ 3

How can I solve this code with a JS loop

You’ll understand that I mean once you open the code: There must be only one section showing up at a time. there are x amount of sections. Inside each of those sections there are buttons (‘.prev’, ‘.next’). let’s say on section1, if you click on ‘.next’, the next section shows up (section2). Etc. ————- the problem, for some reason I can’t get the ‘prev’ button to work well because i also need to get the variable which tracks which section is active https://code.sololearn.com/WemEIXTm4DxW/?ref=app

2nd Aug 2021, 5:10 AM
Ginfio
Ginfio - avatar
6 Answers
+ 4
Ginfio 👍 Another approach would be like this: https://code.sololearn.com/W80l0huYVkI5/?ref=app
4th Aug 2021, 5:19 AM
Gordon
Gordon - avatar
+ 3
Gordon after pulling 186 hairs out, i figured out I will share the code with you tomorrow.
3rd Aug 2021, 7:12 AM
Ginfio
Ginfio - avatar
+ 3
Gordon here is how I solved it. I removed the nextCurrent variable. inside the if {} loop current++ prevCurrent = current - 1; instead of $(...prevCurrent...).addClass("active"); $(... + current ...).removeClass("active"); now it is $(...prevCurrent...).addClass("active"); $(... + current ...).removeClass("active"); same thing for when prevBtn is clicked. https://code.sololearn.com/WemEIXTm4DxW
3rd Aug 2021, 5:46 PM
Ginfio
Ginfio - avatar
+ 2
You overcomplicated the logic. For the callback functions, you only need to update the current, and then call a updateDisplay function, in which: - remove active for all section - add active for current section. Here is a demo: https://code.sololearn.com/WrHLer4YI7S0/?ref=app
2nd Aug 2021, 6:47 AM
Gordon
Gordon - avatar
+ 1
Gordon I’m having a hard time understanding your code| I’ve yet got to study jquery could you show me how you do that with the the button clicks (prev, next). ok my code perhaps https://code.sololearn.com/WemEIXTm4DxW/?ref=app thx
2nd Aug 2021, 5:19 PM
Ginfio
Ginfio - avatar
+ 1
Ginfio sure, i'll shoot a public youtube video to explain, if you don't mind
3rd Aug 2021, 5:18 AM
Gordon
Gordon - avatar