5. Events
Table of Contents
Event Driven Programming: Listen and React
Imagine a website with a button. Each time you click on the button, something happens (maybe the color changes!). How would you program something like this?

In event-driven programming, we instruct our program to listen for events and react when the event is triggered.
An event can be triggered by:
clicking a button
moving your mouse
pressing a key on your keyboard
the webpage finishes loading
the window is resized
the user scrolls down the page
In JavaScript (and many other languages), we set up our program to react to events by "registering" a callback function called an event handler that is tied to an element and an event type.
addEventListener
The addEventListener method is available on all elements in the DOM and is invoked with two values, an event type string and an event handler callback.
A single element can have multiple event listeners / event handlers.
Event Type
The first argument of .addEventListener() is a string that defines the event type to listen for such as:
"click"- an element was clicked"mousemove"- the mouse moved over an element"keydown"- a key was pressed down"keyup"- a key was released"submit"- a form was submitted"input"- thevalueof aninput,select, ortextareahas changed
You can find more information about Events on MDN.
Event Handlers and the event Object
event ObjectThe second argument of addEventListener is an event handler, a callback function that is invoked when the specified event fires "on" the given element.
The handler will be invoked by addEventListener with an event object as an input. This event object has many useful properties / methods about the event, like the event.type and event.target:
These two properties are perhaps the most important. They are on every event object regardless of the event type:
event.target— the Element that triggered the event.event.currentTarget— The Element that is is handling the event (often the same asevent.targetbut can also be different. See event delegation below).
Challenge
Suppose you had this event handler:
How would you trigger it to be invoked whenever a key was pressed anywhere on the page? What about if you moved your mouse over an element with the id mouse-area?
Good to Be Aware of, Not to Use: Inline Handlers
You can also define event handlers inline directly in HTML:
This is good to be aware of for when we get to React but you should NOT use this since we want to keep our JavaScript logic in our .js files and out of .html files.
Event Propagation
Propagation: the act or process of spreading something
Imagine we had the following structure:
When an event is triggered by an element (e.g. a button is clicked), that element and all of its parent elements can "hear" that event and can listen to/handle those events. We call this event propagation or bubbling.
This means that an event listener added to div#middle and div#outer will be triggered when we click on the button#inner element.
event.targetis the Element that triggered the eventevent.currentTargetis the Element handling the event
With event propagation, the element handling the event (event.currentTarget) will be a parent of the event that triggered the event (event.target)
To prevent events from bubbling up, use the event.stopPropagation() method available on all events:
Q: What would happen if we removed the event handlers for #inner and #middle?
Event Delegation
Delegation: the act of empowering to act for another.
Event propagation/bubbling allows a really powerful design pattern called event delegation. Suppose you had the following list:
Each list item has a picture and a solid black border. As long as we have our mouse hovering over on an image, we want the border of that image (and only that image) to turn red! We can do that with an event listener like this:
Now, to get that to work for all of our images, one solution would be to add mouseover and mouseout event handlers to every single image...
...but that looks kind of awful. If we had 100 images, then we'd need 200 event listeners... 🤮
Instead, we can just add the event listener to the container, the ul#picture-list. This requires one important tweak: we have to make sure that only events triggered by the img elements themselves are handled with a guard clause
Pretty neat, right?!
Removing Event Listeners
One of the reasons why passing a named callback function to your listeners is better is because you can then remove them if you need to.
We remove event listeners to limit user interactions and also be 100% sure that we aren't committing memory leaks when we remove elements. (However, modern browsers are pretty good at cleaning up after us).
Q: Why can we write the removeListenerButton event listener as an inline arrow function but we can't for the counterButton event listener?
Last updated