The first time the for loop starts (with i=0 ), it goes to the return , and the function stops working after one iteration, never reaching the last part of the script.
This can be done with less code if you just modify the HTML a bit by putting the value as an argument in enterPin :
<input type="button" class="numKey" id="key7" value="7" onclick="enterPIN(7);">
Or, as bcdan suggested, using this , so you don't need to repeat:
<input type="button" class="numKey" id="key7" value="7" onclick="enterPIN(this.value);">
Notice that I changed from submit to button , since you really don't want to submit the form after clicking the buttons. Then you just need this JS:
function enterPin(number) { screen = document.getElementById("screen"); screen.innerHTML = screen.innerHTML + String(number); }
Or if you want to use jQuery (and get rid of the onclick attribute):
$(".numKey").click(function() { screen = $("#screen"); screen.html(screen.html + this.value); });
source share