---
title: "Slide Show in JavaScript"  
description: "Introduction:In this blog I am going to explain how we can change the image on a button click.Example:html xmlns=\"http://www.w3.org/1999/xhtml\"  head"  
author: "Danish Khan"  
published: 2012-11-09  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/388/slide-show-in-javascript  
category: "javascript"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# Slide Show in JavaScript

##### Introduction:

In this [blog](https://www.mindstick.com/articles/12705/myths-and-misconception-about-blog) I am going to [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) how we can change the [image](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) on a [button click](https://www.mindstick.com/forum/790/form-gets-submiited-twice-on-button-click).

##### Example:

```
<html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>Slide Show</title>    <script type="text/javascript">        var pics = new Array("Chrysanthemum.jpg", "Tulips.jpg", "ball.jpg");        var picCount = 0;        function PreviousImage() {            if (document.animatePics && picCount == 0) {                alert("You are on first slide");            }            else if (document.animatePics && picCount > 0) {                picCount--;                document.animatePics.src = pics[picCount];            }        }        function NextImage() {            if (document.animatePics && picCount == 2) {                alert("You are on last slide");            }            if (document.animatePics && picCount < 2) {                picCount++;                document.animatePics.src = pics[picCount];            }        }    </script></head><body>    <p>        <img name="animatePics" alt="" src="Chrysanthemum.jpg" style="height: 90px; width: 120px" />        <input id="btnPrevious" type="button" value="Previous" onclick="PreviousImage()" />        <input id="btnNext" type="button" value="Next" onclick="NextImage()" /></p></body></html>
```

##### Output:

![Slide Show in JavaScript](https://www.mindstick.com/blogs/84de62f0-b607-422f-9ac9-9fe6901e85d1/images/2581a2b6-966c-4b36-aee5-20d29dca5dcc.jpg)\
\

When the previous button is pressed then if the image location is not at 0 position in the array then the counter is decremented and the image at just the previous location is shown . When the user presses the Next button then if the image is not last in the array location then the counter is incremented and the next image is shown.

Conclusion:

In this blog I explained about the concept of slideshow in JavaScript. How to change the image when the button is pressed.

\

---

Original Source: https://www.mindstick.com/blog/388/slide-show-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
