When ever I try to click on the image, the image changes but the next image in the array is not displayed
<!doctype html>
<html>
<head>
<title>
slides
</title>
<script type="text/javascript">
function nextslide(){
var images = new Array()
images[0]= "home.jpg"
images[1]= "left.jpg"
images[2]= "right.jpg"
var currentpic=0
var lastpic= images.lenth-1;
if (currentpic =lastpic)
{
currentpic=0;
document.getElementById('slide').src = images[currentpic];
}else
{
currentpic++;
document.getElementById('slide').src = images[currentpic];
}
}
</script>
</head>
<body>
<img src="home.jpg" id="slide" onclick="nextslide()">
</body>
</html>
Help would be greatly appreciated. Thank you for the help.
try this
1.) Specify globallythis variable
2.) Change in images.lenth to images.length
3.) Change if (currentpic =lastpic) to if (currentpic ==lastpic)
There are several things wrong with your code. Here is the fixed version:
What's wrong?
var lastpic= images.lenth-1;
You're missing ag
inlength
.if (currentpic =lastpic)
To check ifvar1
is the same asvar2
, you need to use==
instead of=
currentpic
,images
, andlastpic
outside of your function to make it actually set the image as the next image.To try and debug yourself
Always check your browser's developer console for errors.