Skip to main content

Tales From the Other Side: e.stopPropagation();

 Hello Readers,

    Greetings from a chilly November day in New York. The kind of day that forces you to forget that Fall is after Summer, and to remember that Fall is before Winter. The kind of day that makes one glad they spent several years in Vermont and now have several tricks up their sleeve. Namely: warm socks and vigorous walking. But I digress.
    The first 'tale from the other side' that I wanted to share is actually the one I was most excited about (in real time!) when I learned about it. Spoiler alert from the title: it's the event.stopPropagation() javascript method. So...what the heck is it?
    Well, here's a few things about it:

  1. It's a native Javascript method, so there's no extra libraries, packages, components, syntaxes, etc to import.
  2. It's in the same 'family' as event.preventDefault(), so those familiar with other common event handlers may already have a sense of where it can be called.
  3. Think about all the ways you could want your webpage to 'handle' an event; before I knew this was an existing method I could already see a use for it.
    Fun fact: given my own limited knowledge of the libraries of Event Listeners and Event Handlers, etc, I worried that it would be something I needed to build from scratch and began stressing over such an undertaking. Wonders never cease; thankfully somebody thought of it long before me.

    So, what was this use case I encountered? The one that allowed me to see the need for such an Event Handler to exist in the first place? To hammer a point home: I am fairly confident this learning opportunity would not have existed for me if I were not on 'the other side.' Working with the company's product owner, as well as designer, I was asked for a deliverable that was outside of my experience. Namely: I was asked to, in case the product was sold out, disable the onClick functionality in the associated 'Add to Cart' button element - and have no other Event Listener 'handle' the event either.


hastily rendered image to demonstrate the situation, with the bottom button being the object in question
 
    Setting a conditional to eliminate the onClick functionality of the button was simple. Here's what was less simple: getting the button to have no functionality in an environment (ie, the Product 'Card') which had its own, separate onClick functionality covering its entire surface! In trying to figure out how I would attack this problem, I began visualizing the situation in layers: a visual, vertically-oriented representation for the nested HTML tags, and the inherent hierarchy of functionality therein.

like so

    What I needed, I reasoned, was to maybe carve out the chunk of space 'under' the button element, to create some kind of 'Functional Dead Zone'. That way, if the onClick functionality of the element needed to be disabled, the event would not automatically be 'handled' by the element "below" it in the functional hierarchy and the user would see...nothing! A worthy goal indeed

like so


    But, I dreaded: how would I actually go about that?
    Fortunately, before I could waste literally any time, the Senior Engineer on my team turned me on to the brilliant, time-saving(, life-saving?) e.stopPropagation method. It would, he explained, not only prevent the Event Handler from firing on the button element - it would stop any other listening from firing too. ("A functional dead zone?", I thought to myself, in awe).
    One other, somewhat funny thing occurred to me while discussing the problem with the Senior Engineer: he kept referring to DOM events as "bubbling up," and saying that employing
e.stopPropagation would stop the event from "bubbling up" the chain of Event Listeners. "No!," I kept thinking, "it would stop the event from burrowing down the layers of HTML elements and functional hierarchy. For the record, I kept this comment to myself, because I certainly got his point. Funny enough, W3Schools describes the Event propagating in both directions, with e.stopPropagation putting an end to any movement. It really is a Functional Dead Zone!

    From there the rest of the component came together like clockwork. With that method I was able to allow the user to experience a Product Card that, when clicked on anywhere, would open the associated modal with that product's information, with the important exception of: if the user clicked on the Add to Cart button, it would add the Product to the Cart. And, as I had set out to achieve: if the Product was Sold Out, and the user clicked on the Add to Cart button...nothing would happen, and the DOM would not open the product information modal!

    It was a very satisfying day for me on the 'other side.' Stay tuned for more tales!

Comments

Popular posts from this blog

Don't Touch That Dial

    This article was inspired by a technical error I can't replicate through writing, nor refer to in official documentation (unless I missed something ). The overhead digital projector in one of the classrooms I attend, in between displaying its computer signal and a neutral, soothing blue screen displayed something curiously familiar: tv static. you may be familiar...     For a brief instant, the "snow" felt oddly comforting. It made me think of old movies in the basements of old houses. It reminded me that  Hunter Thompson liked to fall asleep to white noise  (white noise, side note, actually having some  therapeutic benefit? ). It made me wonder if my teacher needed to climb on the roof and adjust the antenna.      Then it hit me: what the heck? Technically speaking/as this  helpful reddit page  explains, tv static was originally the cause of an internal mechanism within the television: the amplifier, normally in charge of boosting whatever signal it received (from th

Introducing: Tales from the Other Side

      Hello to my loyal fans. I hope you all have been having an excellent autumn/summer/spring/2022 since you last stopped by, hoping to find what was instead a mysteriously (and no doubt, frustratingly) absent fresh article from me. Apologies for keeping you waiting. But here it is!     Still, I hear you. "What", you might be asking, "could be so important that it would be worth pausing on committing to record these written anecdotes that so many of us have come to enjoy?". Well, I'll tell you what - I got a job! I got a job! I got a job!     Well, sort of. I got an internship. For several months this summer-through-fall I worked as a member of the Frontend Web Team at the Meal Kit delivery company Blue Apron . I was pretty excited for the opportunity. Not only would it be my first job in tech, but the food industry in general is something I've been an enthusiastic part of for most of my professional life. Not only was I excited to retain some semblance of

Since I Been Gone

Since I been gone / I can breathe for the first time / So I'm trekking on, yeah, yeah / Thanks to you, GROW Externships -Kelly Clarkson, paraphrasing     Hello Loyal Readers. Happy New Year(!). What have I missed? Seriously, as far as I can tell it was some pretty uneventful days around the homefront. But, I can hear you all asking: where have you been, Max? Well, I was out of town. Actually, I was out of country - and in Japan!   Actually, what a place to spend December 7th...       So, how did I get there? Although I did ironically visit Matsue - the entire City of which, from the mayor on down, is encouraging learning and building with Ruby - I was unfortunately not brought to Japan for programming reasons. Which is to say - I was, very fortunately, brought to Japan, by a company for other reasons. Specifically: my agricultural skills.     For those who don't know me, a little background may be of benefit: at one time I was a farmer. I had my own back-forty , flock of chic