2021年4月28日星期三

Prevent listener from firing

I'm working on a script that listen to user clicks and define its type (either Simple,Link change or form submit), I'm doing this through Listeners, but the click listener is triggered with every click. I don't want it to fire when one of the other two is fired

I tried using event.preventDefault and then check using event.defaultPrevented still it's firing.

This is my click listener:

document.body.addEventListener('click',function(event){    if (event.defaultPrevented) return;  console.log('click')  })  

this is my beforeunload event listener:

window.addEventListener("beforeunload", function (e) {      e.preventDefault();      console.log('before unload');  })    

And this is my submit listener:

Array.from(document.querySelectorAll('form')).forEach((element,index) =>    {      const submitButton = element.querySelector('[type=Submit]');        submitButton.addEventListener('click', function(event){         event.preventDefault();          console.log('submit clicked');            },true);    });  

Is there something i'm doing wrong to stop the first listener from firing?

https://stackoverflow.com/questions/67310916/prevent-listener-from-firing April 29, 2021 at 12:08PM

没有评论:

发表评论