this code was bothering me the whole day. Lets say ledCount = 9, the code gets the elements by ID without any problem, but since it would have to bind a seperate function onClick, and since the variable i is local, the writeLED function always gets the first parameter 10 (which is max i+1), but it would need to get the current i+1, like the getElementBy id i+1 does. Anyone can solve the puzzle?
function showLED(ledCount){
for(var i = 0;i<=(ledCount-1);i++){
if(color[i] == 0){
document.getElementById('buttonLED'+(i+1)).onclick = function(){writeLED((i+1),1); } ;
document.getElementById('buttonLED'+(i+1)).value="light is on";
}else{
document.getElementById('buttonLED'+(i+1)).onclick = function(){writeLED((i+1),0); } ;
document.getElementById('buttonLED'+(i+1)).value="light is off";
}
}
}