I’m trying to make a site that will switch between scanned images of the pages of my sketchbook using 2 different buttons on either side of the image. All of the URLs of the images are the same except for the page number in the URL. I have been attempting to use Javascript to make the buttons change the number in the URL to change the image.
I succeeded in making Javascript provide the number in the URL for the image, however, no matter WHAT I’ve tried, the buttons refuse to actually change the number. Even though it feels like something so simple should be easy.
I’ve tried various if/else statements. I’ve tried a while/do statement. I’ve tried "pageNumber++" and "pageNumber+=1". I learned the basics of JQuery to see if it would help in any way only to realize that the problem that it solves is not the problem I’m having. And frankly, I’m starting to feel pretty upset.
This is the basics of my setup minus any button functions:
<div> <--! This div is also a flexbox btw -->
<button><h3>Previous</h3></button> <--! There's nothing happening with this button because I was just trying to figure out the other one first -->
<a href="" id="pagelink" target="_blank"><img src="" height="400px" id="page-holder"></a>
<button id="nextbutton"><h3>Next</h3></button>
</div>
<script>
let pageNumber = 1;
let currentPage = "https://example.com/Images/Art/Sketchbook Page " + pageNumber + ".png";
const pageHolder = document.getElementById('page-holder');
const pageLink = document.getElementById('pagelink');
const nextButton = document.getElementById('nextbutton');
pageHolder.src = currentPage;
pageLink.href = currentPage;
//Input all of the different button functions I tried here
</script>
If anyone has any idea of how I can successfully do this, that would be greatly appreciated.
Btw, I’m also using http://www.w3schools.com/lib/w3data.js to help with having the same html pieces on multiple sites without having to go and update them all individually, which I don’t think would be interfering with this, but I don’t even know anymore 🙁
2
Answers
You have to specify "what to happen" when clicking a specific element. Since You haven’t done that all the JS code you have written here will be executed as soon as the page is loaded. In this case, You have to add an EventListener to your
Previous
andNext
buttons, and you have to write your logic inside of the callback function.You can implement cycling forward and backward through a list of image URLs by storing the URLs in an array and keeping track of the current image URL index. When you want to update the image source to the next image, modify the index to the next valid array index and then change the source. To go backward, subtract from the index instead.
Below is a reproducible example that you can use. It includes comments at each step to help explain. Additionally, here are some links to documentation for some of the APIs that are used:
Array.prototype.map()
Remainder operator (
%
)EventTarget:
addEventListener()
method