Saturday, October 14, 2023
HomeSoftware DevelopmentJavaScript Occasion.defaultPrevented

JavaScript Occasion.defaultPrevented


Whether or not you began with the previous on_____ property or addEventListener, that occasions drive consumer experiences in fashionable JavaScript. Should you’ve labored with occasions, that preventDefault() and stopPropagation() are ceaselessly used to deal with occasions. One factor you in all probability 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 press 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. On account 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 without having to trace monitor it globally!

  • Conquering Impostor Syndrome

    Two years in the past I documented my struggles with Imposter Syndrome and the response was immense.  I obtained messages of help and commiseration from new internet builders, veteran engineers, and even individuals of all expertise ranges in different professions.  I’ve even caught myself studying the put up…

  • Send Text Messages with PHP

    Youngsters nowadays, I inform ya.  All they care about is the know-how.  The video video games.  The bottled water.  Oh, and the texting, all the time the texting.  Again in my day, all we had was…OK, I had all of these items too.  However I nonetheless do not get…



Supply hyperlink
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments