Reputation: 159
I am having a problem with a quite basic bit of javascript to call a function which is just a test alert to check the code I have so far is working. I am getting "Uncaught ReferenceError: echo is not defined at HTMLButtonElement.onclick (index.html:23)" in the Chrome console. Here is my index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simple Calculator</title>
</head>
<body>
<header>
</header>
<nav></nav>
<main>
<table style="width:100%">
<tr> <th>Fancy Calculator</hd></tr>
<td>
<form>
<label for="number">Please enter a number between 0 and 50.</label><br>
<input type="text" id="number" name="number"><br>
<button type="button" onclick="echo(number.value)">Calculate!</button><br>
<input type="text" id="factorial" name="factorial" disabled>!<br>
<input type="text" id="squared" name="squared" disabled>²<br>
<input type="text" id="cubed" name="cubed" disabled>³<br>
</form>
</td>
</tr>
</table>
</main>
<footer>
<!--- Script 2.1 - template.html -->
<script src="js/calc.js">
</footer>
</body>
</html>
Here is my calc.js file:
/* calc.js */
console.log("Hello");
function echo( number ) {
alert("You entered", + number + "!");
}
Any help would be much appreciated.
Upvotes: 2
Views: 2472
Reputation: 903
You added an unnecessary comma to your function.
Try this:
function echo(number) {
alert("You entered: " + number + "!");
}
And, as Quentin mentioned, make sure the URL to your script is correct.
Also, I think <script>
elements need to be closed using </script>
, so add that on the end of your script to see if it makes a difference.
<script src="js/calc.js"></script>
Upvotes: 1