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

Time:09-08

i have to anderstand whats rong with my function

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

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

CodePudding user response:

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();
        }

CodePudding user response:

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>

  • Related