I'm trying to create a number game for a class assignment. The computer generates a random number and the player gets five chances to guess it. I've completed all the requirements for the assignment but I want to modify the game so that it will accept a guess by pressing the Return/Enter key (on the keyboard) , not just the button. I have been looking at Stackexchange (especially Trigger a button click with JavaScript on the Enter key in a text box) but I cannot get it to work. I don't want to use JQuery. Any suggestions would be appreciated.
Here is my code:
<head>
<script type = "text/javascript" src = "Guessing6.js"></script>
</head>
<body>
<form action="Guessing6.js" method="get">Enter a number between 1 and 100: <br />
<input type="text" name="inputbox" onkeydown="if(event.keyCode==13) document.getElementById('mybutton').click()"/>
<input type="button" id="mybutton" value="Guess" onClick="testResults(this.form)">
</form><br />
</body>
And the relevant js:
function testResults (form) {
var TestVar = form.inputbox.value;