I have 4 input boxes (streamInput1-4) and 4 buttons (button1-4) and I'm trying to use a loop to assign each button to its corresponding input box, but for some reason the buttons won't click when I use a concatenated element id with the .click() method.
This code works:
document.getElementById("streamInput1")
.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode == 13) {
document.getElementById("button1").click();
}
});
But this code does not:
var numOfPlayers = 4;
var ii;
for ( ii = 1 ; ii <= numOfPlayers ; ii++ ) {
document.getElementById("streamInput"+ii)
.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode == 13) {
document.getElementById("button"+ii).click();
}
});
}
I found that using "button1" in place of "button"+ii will work but "streamInput1" in place of "streamInput"+ii won't work. So only the concatenated element id with .click() isn't working properly.
Am I doing something wrong?