Alexandre Thebaldi February 2016

How check if custom event was prevented

In my code i need to interact with custom event after dispatched

// ...
let customEvent = new CustomEvent('myevent', {
  bubbles: true,
  cancelable: true
});

button.addEventListener('click', function (e) {
  e.preventDefault();
  otherElement.dispatchEvent( customEvent );
  // at this point i neeed some help
  // if ( customEvent was canceled or prevented ) { do something }
});

How can check if it has been prevented/canceled or another solution to get some "feedback" from event?

Answers


epascarello February 2016

You can check for the Boolean defaultPrevented in the event Object


Bergi February 2016

You can check for the defaultPrevented property. Also the dispatchEvent method returns whether the default action is still to take place or not.


Taran J February 2016

lots of ways to do this. one way:

var isCanceled = !otherElement.dispatchEvent(event);

Post Status

Asked in February 2016
Viewed 1,614 times
Voted 4
Answered 3 times

Search




Leave an answer