What is NEMT?

Medicaid NEMT or Non-Emergency Medical Transportation is a phrase typically used to describe a transportation benefit for Medicaid or Medicare members that ensures eligible patients can get to and…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




JavaScript Event Propagation

The biggest revelation I had while learning JavaScript was the concept of bubbling or capturing events. In theory, it’s simple: when I click something on the page (an element), the browser goes through the element’s ancestor chain to find event handlers. The event handlers then do something in response to the click.

In practice, propagation felt much more complicated. I spent more time than I care to admit trying to understand why an event handler didn’t fire when I expected it to, and building long, convoluted, nested chains combined with query selectors to access data on the event object itself.

Painstakingly solving each of these problems and discussing the issue with my instructor led me to the (embarrassing) realization that even if the event listener is on the parent class, the browser knows exactly which element triggered the event (event.target).

Capturing, on the other hand, is rarer. It runs in the opposite direction of bubbling, first checking <html> for event handlers, and so on, until it reaches the element that triggered the event.

To reiterate for myself and other novice programmers, this means that you can place an event handler on the parent element. While there are times when it would make sense to add separate event handlers on each child element, a solitary handler on the parent can be much simpler.

Understanding this property gives you much more control over your events and event handlers, and can simplify the way in which you manipulate the DOM as a result. event.target is the element that triggered the event, whereas event.currentTarget is the element that has the event handler.

For example:

My event listener, however, is attached to the calendar itself (event.currentTarget). Anytime a user clicks a plus sign on the page, the browser knows exactly which plus sign is being triggered (event.target), and therefore exactly which day should be updated.

Wow! No matter which button a user clicks, the resulting note will be added to the matching day. Harnessing event propagation makes this behavior clear and straightforward.

Add a comment

Related posts:

Not all endurance is the same

You get good at what you train for, but without variance don’t expect the results of one event to translate well to another.

Configure static IP address on Ubuntu 20.04

Ubuntu 18.04 LTS and Ubuntu 20.04 LTS has switched to Netplan for configuring network interfaces and it’s installed by default. Netplan is YAML based configuration system.

How to Grow your Instagram Page?

Instagram has more than 1.074 billion active users worldwide in 2021. 71% of the 1.074 billion monthly active users on the Instagram platform are