Hundotte Hundotte - 7 months ago 13
HTML Question

Why is function not defined?

I have a html5 and javascript game as follows:

<title>Garrid Punching</title>
canvas {
border: 1px solid black;
background-color: black
function answer(e) {
if (e.keyCode === 13) { //enter key pressed
alert("it worked")

<body onkeypress="return answer(event)">

<canvas width="1080" height="480" id="myCanvas"></canvas>


What is expected to happen is that when the enter key is pressed, Javascript receives the event and then interprets the keycode to be equal to 13, causing it to send an alert that says "It worked".

However, when I press enter nothing happens, and it instead returns the problem "function 'answer' not defined" in the debugger.

Why would this be so?


I tried this in Chrome and it works absolutely fine. What browser are you using to test this ?

Try removing the return keyword and call the function right away.

<body onkeypress="answer(event)">