Friday, February 3, 2023
HomeSoftware DevelopmentJavaScript Occasion.defaultPrevented

JavaScript Occasion.defaultPrevented

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

Contemplate 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 press has been dealt with
    else {
        // In any other case do one thing recent

When preventDefault known as on a given occasion, the defaultPrevented property will get toggled to true. Attributable to 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!

  • Introducing MooTools Templated

    One main downside with creating UI elements with the MooTools JavaScript framework is that there is not an effective way of permitting customization of template and ease of node creation. As of in the present day, there are two methods of making: new Aspect Insanity The primary strategy to create UI-driven…

  • 7 Essential JavaScript Functions

    I keep in mind the early days of JavaScript the place you wanted a easy operate for almost the whole lot as a result of the browser distributors carried out options otherwise, and never simply edge options, primary options, like addEventListener and attachEvent.  Instances have modified however there are nonetheless a number of capabilities every developer ought to…

  • CSS Custom Cursors

    Bear in mind the Internet 1.0 days the place you needed to customise your web site in each approach potential?  You abused the scrollbars in Web Explorer, in fact, however the most well-liked exterior service I can keep in mind was CometCursor.  CometCursor allow you to create and use a great deal of customized cursors for…

  • Create Spinning Rays with CSS3: Revisited



Please enter your comment!
Please enter your name here

19 − 19 =

Most Popular

Recent Comments