beenstuckonthiscodeforever beenstuckonthiscodeforever - 4 months ago 52
Javascript Question

traffic light sequence lights won't change

I am trying to create a traffic light simulation using html and JavaScript. I managed to get the starting image to display but the button just doesn't work.


just take this (you ought to run this with Internet)


<p>Click the button to change to the next light.</p>

<img id="starting_light" src="">

<button type="button" onclick="nextLightClick()">Next Light</button>


var lights = new Array("","","");

var index = 0;
var lightsLen = lights.length;

function nextLightClick() {

    if (index == lightsLen) 
        index = 0;

    var image = document.getElementById('starting_light');
    image.src = lights[index];