Image source that can loop on array - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image

Description

Image source that can loop on array

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <script type="text/javascript" language="JavaScript">
var i =0;//from  w w  w  .j a  v a2  s .  c o m
var trafficarray=["1.gif","2.gif","3.gif","4.gif"];
var image = document.getElementById("trafficlights");
function cycle()
{
  i+=1;
  document.getElementById("trafficlights").src=trafficarray[i]
  if (i>2) {i=-1};
}

      </script> 
   </head> 
   <body> 
      <img src="1.gif" id="trafficlights"> 
      <button onclick="cycle()">click me</button>  
   </body>
</html>

Related Tutorials