You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: page/events/event-delegation.md
+37-23Lines changed: 37 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -6,26 +6,30 @@ attribution:
6
6
- jQuery Fundamentals
7
7
---
8
8
9
-
Say you have to add new line items to your page, given the following HTML:
9
+
## Introduction
10
+
11
+
Event delegation allows us to attach a single event listener, to a parent element, that will fire for all children matching a selector, whether those children exist now or are added in the future.
12
+
13
+
## Example
14
+
15
+
For the remainder of the lesson, we will reference the following HTML structure:
We need to attach the same event handler to multiple elements. In this example we want to attach an event that will log the text of the anchor tag to the console whenever it is clicked.
31
+
When an anchor in our `#list` group is clicked, we want to log its text to the console. Normally we could directly bind to the click event of each anchor using the `.on()` method:
27
32
28
-
We can attach a direct bind click event to each `<li>` using the `.on()` method, that will alert the text inside of it by doing the following:
If we were to click our newly added item, nothing would happen. This is because of the directly bound event that we attached previously. Direct events are only attached to elements at the time we called the `.on()` method for our existing collection of `<a>`'s, that is only the `<a>`'s that were found when we call `$()`.
47
+
48
+
If we were to click our newly added item, nothing would happen. This is because of the directly bound event handler that we attached previously. Direct events are only attached to elements at the time the `.on()` method is called. In this case, since our new anchor did not exist when `.on()` was called, it does not get the event handler.
43
49
44
50
## Event Propagation
45
-
Understanding how events propagate is an important factor in being able to leverage Event Delegation. Any time an anchor tags is clicked, a *click* event is fired for the:
51
+
52
+
Understanding how events propagate is an important factor in being able to leverage Event Delegation. Any time one of our anchor tags is clicked, a *click* event is fired for that anchor, and then bubbles up the DOM tree, triggering each of its parent click event handlers:
46
53
47
54
*`<a>`
48
55
*`<li>`
49
-
*`<ul>`
50
-
*`<div>`
56
+
*`<ul #list>`
57
+
*`<div #container>`
51
58
*`<body>`
52
59
*`<html>`
53
60
**document* root
54
61
55
-
Anytime one of these links is clicked you can think of it as if you were clicking the entire document body. This is called *event bubbling* or *event propagation*.
62
+
This means that anytime you click one of our bound anchor tags, you are effectively clicking the entire document body! This is called *event bubbling* or *event propagation*.
63
+
64
+
Since we know how events bubble, we can create a *delegated* event:
56
65
57
-
Since we know how events bubble we can created a delegated event that listens for a specific event to happen on our element
Notice for the second parameter to the `.on()` method we are telling it which selector to listen for. Now when a *click* event is triggered on our `<ul>`, our delegated event will check to see if the triggering element matches our selector (`"a"`). If it does, our anonymous function will execute. We have now attached a single *click* event listener to our `<ul>` instead of an unknown number of directly bound events on our `<a>`'s.
66
73
67
-
Now lets say that whenever a link is clicked we want to check and see if the `href` attribute starts with "http" and if it does we want to set the `target` attribute to `_blank`.
74
+
Notice how we have moved the `a` part from the selector to the second parameter position of the `.on()` method. This second, selector parameter tells the handler to listen for the specified event, and when it hears it, check to see if the triggering element for that event matches the second parameter. In this case, the triggering event is our anchor tag, which matches that parameter. Since it matches, our anonymous function will execute. We have now attached a single *click* event listener to our `<ul>` that will listen for clicks on its children anchors, instead of attaching an unknown number of directly bound events to the existing anchor tags only.
75
+
76
+
### Using the Triggering Element
77
+
78
+
What if we wanted to open the link in a new window if that link is an external one (as denoted here by beginning with "http")?
This simply passes the `.is()` method a selector to see if the element's `href` attributes starts with "http". Also we have removed the `event.preventDefault();` statement, this is because we want the default action to happen (which is to following the `href`)
78
89
79
-
We can actually take this a step further and make our code simpler and more concise by allowing the selector argument to `.on()` do our logic for us.
90
+
This simply passes the `.is()` method a selector to see if the `href` attribute of the element starts with "http". We have also removed the `event.preventDefault();` statement as we want the default action to happen (which is to follow the `href`).
91
+
92
+
We can actually simplify our code by allowing the selector parameter of `.on()` do our logic for us:
93
+
80
94
```
81
95
// attach a delegated event with a more refined selector
Event delegation refers to the process of using event bubbling to handle events at a higher level in the DOM than the element on which the event originated. It allows us to attach a single event listener for elements that exist now or in the future.
102
+
Event delegation refers to the process of using event propagation (bubbling) to handle events at a higher level in the DOM than the element on which the event originated. It allows us to attach a single event listener for elements that exist now or in the future.
0 commit comments