Trying to call a javascript function using a button, and the button does nothing when pressed. The debugger returns an "unsached reference error: printNumber" undefined.
printNumber is the function I want to call.
I did a search, and most people with this problem did not use the correct syntax to declare their function, defined their function within another function (functions outside the scope), or put their functions after they were called. It turns out that nothing works in my head. For example, I tried to include an invitation in it, and the invitation did not appear.
Here is my script:
<head> <script type="javascript"> var x=0; function addNumber(x){ x = x + 1; return x; } function printNumber(number){ document.getElementById("number").innerHTML=addNumber(number); } </script> </head>
Here is my HTML:
<body> <p><center><b>The number of times the button has been clicked is ... <b><p> <br> <div id="number"></div> <br> <form> <input type="button" id="StartButton" value="Click to add 1 to counter" onClick="printNumber(x)"> </form> </center>
Can someone point me where this problem comes from? Thanks, this is for the programming class, and I hope others who may also come across this will see this.
source share