I want to create a slider in Javascript with two buttons 'Next' and 'Previous'. The next button is working but Previous is not. Here is my code:
let myImage = document.getElementById('mainImage');
let images = ["./img/th01.jpg", "./img/th02.jpg", "./img/th03.jpg", "./img/th04.jpg", "./img/th05.jpg"];
let imageIndex = 1;
function next(){
myImage.setAttribute('src', images[imageIndex]);
imageIndex++;
if (imageIndex >= images.length) {
imageIndex = 0;
}
}
function previous(){
myImage.setAttribute('src', images[imageIndex]);
imageIndex--;
if (imageIndex <= 0) {
imageIndex = images[imageIndex];
}
}
<div id="wrapper">
<img src="./img/th01.jpg" id="mainImage">
<br>
<br>
<button onclick="previous()">Previous</button>
<button onclick="next()">Next</button>
</div>
Any help please?