I keep getting an error saying my function "key" is not defined. Here is the HTML code:
<html>
<head>
<link rel="stylesheet" type="text/css" href="interface.css" />
<script language="text/javascript" src="main.js"></script>
</head>
<body onkeypress="key()">
<p>Money: <p id="money">0</p></p>
</body>
</html>
And the JavaScript:
int money = 0;
function key()
{
money += 1;
document.getElementById("money").innerHTML = money;
}
First: text/javascript
is not a language supported by the browser's scripting engine, so the browser will ignore the script.
If you were writing HTML 3.2 you would say:
language="javascript"
If you were writing HTML 4 you would say:
type="text/javascript"
It is 2014, so write HTML 5 and don't specify a type or language attribute at all.
Second, int money = 0;
is invalid JavaScript. (You seem to be trying to write Java.). So the script will throw an exception and stop at that point.
Use var
to declare variables (of any type) in JS.
var money = 0;