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

Viewed 33

i have to anderstand whats rong with my function

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

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

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)

Related