Approach 1:
Bring down URL of all images in first load and then keep changing URL on img tag on click.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>click demo</title>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
var images = [];
images.push("http://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Apple_Headquarters_in_Cupertino.jpg/800px-Apple_Headquarters_in_Cupertino.jpg");
images.push("http://upload.wikimedia.org/wikipedia/commons/2/27/Apple_I.jpg");
images.push("http://upload.wikimedia.org/wikipedia/en/5/5d/Ad_apple_1984.jpg");
images.push("http://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Macintosh_128k_transparency.png/511px-Macintosh_128k_transparency.png");
var currentImageIndex = 0;
</script>
</head>
<body>
<div>
<img id="imgFrame" src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Apple_Headquarters_in_Cupertino.jpg/800px-Apple_Headquarters_in_Cupertino.jpg" style="width:400px;height:400px;" />
<br>
<input type="button" id="next" value="next"/>
</div>
<script>
$( "#next" ).click(function() {
if(currentImageIndex < images.length){
currentImageIndex += 1;
}
else{
currentImageIndex = 0;
}
$("#imgFrame").attr("src",images[currentImageIndex]);
});
</script>
</body>
</html>
Approach 2:
- Create a web service which will give you next image URL.
- Keep track of current image.
- On click of next button perform an AJAX request and get next image url.
- Change img tag URL as shown above.