Wednesday, July 27, 2022
HomeWordPress DevelopmentJavaScript Occasion.defaultPrevented

JavaScript Occasion.defaultPrevented


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

Think about 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);
operate 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 contemporary
    }
}

When preventDefault 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 20 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!

  • Regular Expressions for the Rest of Us

    Eventually you will run throughout a daily expression. With their cryptic syntax, complicated documentation and big 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…

  • Responsive Images: The Ultimate Guide

    Chances are high that any Internet designers utilizing our Ghostlab browser testing app, which permits seamless testing throughout all gadgets concurrently, may have labored with responsive design in some form or kind. And as in the present day’s web sites and gadgets develop into ever extra different, a plethora of responsive photographs…


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments