简体   繁体   中英

Uncaught ReferenceError: echo is not defined at HTMLButtonElement.onclick (index.html:23)

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>&sup2;<br>
 <input type="text" id="cubed" name="cubed" disabled>&#xB3;<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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM