I have a slideshow with button like 1,2,3,4. Also I have define an Array with 4 images url. When click the button, it call the specific corresponding url.
<div id="slideshow">
<div id="source"><img src="images/blue.jpg">
<ul id="controller">
<li class="button">1</li>
<li class="button">2</li>
<li class="button">3</li>
<li class="button">4</li>
</ul>
</div>
</div>
Javascript:
var button=document.getElementsByClassName("button")
var image=document.getElementsByTagName("img")[0]
var sources=new Array("images/blue.jpg","images/red.jpg","images/yellow.jpg","images/green.jpg");
for (var i=0;i<sources.length;i++){
button[i].onclick=function(){
image.src=sources[i]
}
}
But when I click the button, the sources[i] alway return undefined.