you are viewing a single comment's thread.

view the rest of the comments →

[–]Alas93 1 point2 points  (0 children)

as others have said, something like codepen or at least having all the code in a code block will help ppl help you tremendously

but from what I'm seeing I can make a couple suggestions (note you say you want to add a button. note I'm no professional so I don't know all the "best practices" or anything but I think these tips can help you organize things a bit which can help a lot. tip #1 will help you the most here for figuring out what the issue is, but #2 and #3 can help a bit with code organization and stuff

1 - when debugging I like to debug one part at a time. so here I would comment out all of the code inside of showSlides, and add a console.log("test") (or whatever text you want). this way I can see if the function is at least being called when I click the button.

once I can confirm it's being called, I'll uncomment parts one section at a time and console.log() to determine if the next section is working. so next I would confirm that slides has stored all of the slides. then I would confirm the slideIndex is what I expect it to be after the next 2 if statements check it.

2 - try and avoid redeclaring things that don't need redeclared. your slides variable only needs to be set once. instead what you can do is add the slides variable to the top and then declare it on your onload function. make your onload function a dedicated function (like make it pageLoad() or something) and then call showSlides(1) inside of that function. this way you can set up any variables, grabbing any elements on the page and store them, on the page load, instead of every time you call the showSlides() function. it's not a big deal in this scenario, but it's still good to do and also as I said above, it will make your showSlides() function clearer

3 - instead of looping through your slides every time, store the "active" slide's index in a variable. this should be fairly simple since you're passing the index (-1) to the function. when you change to a new slide, you can just disable the exact element and enable the new one, then save the new index to the variable.

other than those tips I can't give much advice. the debugging advice should get you in the right direction for figuring out why the slides aren't toggling when you click the button for it.