In my array, the index numbers are set from 65 to 90 (a-z keycodes). I need to iterate through the array to check if the keynum generated from a keypress event matches one of the index numbers. If it does, I want to change the color of an onscreen button to red and display the corresponding String.fromCharCode in an element's innerHTML.
Assuming my button is like this...
<input type="button" id="a" value="A">
This is the JavaScript code...
document.onkeypress = keypress;
function keypress() {
var buttons = [];
var panel = document.getElementById("panel");
var letter, i;
for(i = 65; i <= 90; i++) {
buttons[i] = document.getElementById(String.fromCharCode(i).toLowerCase());
}
var keynum = (window.event) ? event.keyCode: e.which;
letter = String.fromCharCode(keynum);
for(i = 65; i <= 90; i++) {
if(buttons[i].id.toLowerCase().charCodeAt(0) === keynum) {
buttons[i].style.backgroundColor = 'red';
panel.innerHTML += letter;
}
}
}
Is there a way to make it so that when I press a button, the corresponding onscreen button changes to red? Here is a link to my JS Fiddle... http://jsfiddle.net/AdamMartin121/VXYFC/14/