beenstuckonthiscodeforever beenstuckonthiscodeforever - 1 year ago 130
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.

Answer Source

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];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download