HomeSoftware DevelopmentJavaScript Occasion.defaultPrevented

JavaScript Occasion.defaultPrevented

Whether or not you began with the previous on_____ property or addEventListener, you already know that occasions drive person experiences in fashionable JavaScript. In the event you’ve labored with occasions, you already know that preventDefault() and stopPropagation() are steadily used to deal with occasions. One factor you most likely did not know: there is a defaultPrevented proptery on occasions!

Take into account the next block of code:

// Particular to a hyperlink
const hyperlink = doc.querySelector('#my-link');
hyperlink.addEventListener('click on', e => e.preventDefault());

// A bigger doc scope
doc.addEventListener('click on', documentClickHandler);
perform documentClickHandler(occasion) {
    if (occasion.defaultPrevented) {// Utilizing the property
        // Do one factor if the clicking has been dealt with
    else {
        // In any other case do one thing recent

When preventDefault is known as on a given occasion, the defaultPrevented property will get toggled to true. As a result of occasion propagation, the occasion bubbles upward with this defaultPrevented worth.

I have been dealing with occasions for twenty years and did not know this property existed till now. What’s nice about defaultPrevented is that it stays with the occasion with no need to trace monitor it globally!

  • Convert XML to JSON with JavaScript
  • Regular Expressions for the Rest of Us

    Eventually you will run throughout an everyday expression. With their cryptic syntax, complicated documentation and large studying curve, most builders accept copying and pasting them from StackOverflow and hoping they work. However what when you might decode common expressions and harness their energy? In…

  • Use Elements as Background Images with -moz-element
  • MooTools Kwicks Plugin



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments