Title

Monday, 19 January 2015

Uncaught TypeError: undefined is not a function in javascript


HTML:

<body>  <div id="main">  <span id="msg">Are you sure you want to proceed?</span><br><br>  <input type=button id="Yes" value="Yes">  <input type=button id="No" value="No">  </div>  </body> 

Javascript:

function doTask(){...}  function doNothing(){...}  var do=function(){doTask();}  var nothing=function(){doNothing();}  document.getELementById("Yes").addEventListener("click",do);  document.getELementById("No").addEventListener("click",nothing);

When i run the document, the console shows the Uncaught TypeError: undefined is not a function error on the document.getElementById("Yes")... line. What am i doing wrong?

Answer

why not like this ?

document.getELementById("Yes").addEventListener("click",doTask()); document.getELementById("No").addEventListener("click",doNothing());

Answer2

In your HTML add the <script> tag and put all your JS function inside:

body>  <div id="main">  <span id="msg">Are you sure you want to proceed?</span><br><br>  <input type=button id="Yes" value="Yes">  <input type=button id="No" value="No">  </div>  </body>  <script>  function doTask(){...}  function doNothing(){...}  var do=function(){doTask();}  var nothing=function(){doNothing();}  document.getELementById("Yes").addEventListener("click",do);  document.getELementById("No").addEventListener("click",nothing);  </script>

No comments:

Post a Comment