简体   繁体   中英

I get an error when I try to output the function with the name `dinle()`. The error code is:Uncaught TypeError:

i have to anderstand whats rong with my function

   document.querySelector("#btnClear").addEventListener("click",dinle(event));

        function dinle(event){
            console.log("deneme");
            event.preventDefault();
        }

It seems like event is undefined in your code & or it doesn't have preventDefault Property.

If you wan to use inBuild Event in button, you don't need to pass it as args.Try instead..

// event is removed in the dinle fn call
document.querySelector("#btnClear").addEventListener("click",dinle);

        function dinle(event){
            console.log("deneme");
            event.preventDefault();
        }

event.preventDefault() must be at first line of your function definition and event shall not be the attribute in callback, do the following

 document.querySelector("#btn").addEventListener("click",dinle); function dinle(event){ event.preventDefault(); console.log("deneme"); }
 <button id="btn">CLICK here to run the function </button>

The addEventListener() function takes a type and a listener as parameters. where listener can be a callback function which gets called when the specified type of the event occurs.

see documentation

It seems like you are passing the result of your dinle(event) which would be undefined since it does not return anything.

What is actually causing the Uncaught TypeError is the event parameter, because it cannot be resolved.

Try passing the function itself as the parameter: addEventListener("click", dinle)

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