简体   繁体   中英

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?

why not like this ?

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

Use as

document.getElementById("Yes").addEventListener("click",do,false);
document.getElementById("No").addEventListener("click",nothing,false);

use getElementById in place of getELementById

   document.getElementById("Yes").addEventListener("click",do);
    document.getElementById("No").addEventListener("click",nothing);

and you can also use

<input type=button id="Yes" value="Yes" onClick="doTask();"> 
<input type=button id="No" value="No"  onClick="doNothing();"> 

Tried by replacing L with l from getELementById() and do with don (just a variable name). Worked fine for me.

function doTask(){alert('Yes');}
function doNothing(){alert('No');}
var don=function(){doTask();};
var nothing=function(){doNothing();};
document.getElementById("Yes").addEventListener("click",don);
document.getElementById("No").addEventListener("click",nothing);

Demo

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