Hope I am not breaking Stack Overflow protocol or etiquette here.
I am reposting this solution from another question that was posted.
Original Question: show-and-hide-images-with-next-previous-button-using-javascript
Asked by: user1199537
Prefered solution given by: bunjerd-sparrow
This code works great, but I need it to stop at image 1 (using previous button) and stop at image 11 (using next button), not continuously roll through the images.
I am sure this is light work for you guys. I just can't figure it out to save my life. Any and all help is greatly appreciated.
var $c = 0;
function next() {
var boxes = document.getElementsByClassName("box");
$c += 1;
if ($c >= boxes.length) $c = 0;
for (var $i = 0; $i < boxes.length; $i++) {
boxes[$i].style.display = "none";
}
boxes[$c].style.display = "block";
return false;
}
function prev() {
var boxes = document.getElementsByClassName("box");
$c -= 1;
if ($c < 0) $c = (boxes.length - 1);
for (var $i = 0; $i < boxes.length; $i++) {
boxes[$i].style.display = "none";
}
boxes[$c].style.display = "block";
return false;
}
#container {
position: relative;
width: 120px;
height: 120px;
}
#container div {
position: absolute;
width: 120px;
height: 120px;
}
#box-red {
background: red;
}
#box-yellow {
background: yellow;
display: none;
}
#box-green {
background: green;
display: none;
}
#box-maroon {
background: maroon;
display: none;
}
<div id="container">
<div id="box-red" class="box">DIV1</div>
<div id="box-yellow" class="box">DIV2</div>
<div id="box-green" class="box">DIV3</div>
<div id="box-maroon" class="box">DIV4</div>
</div>
<button onClick="return prev();">previous</button>
<button onClick="return next();">next</button>