Skip to content

Commit 20462df

Browse files
committed
Understanding Event Delegation: Cleanup
1 parent 55d6deb commit 20462df

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

page/events/event-delegation.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ For the remainder of the lesson, we will reference the following HTML structure:
3131
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:
3232

3333
```
34-
// attach a directly bound event
34+
// Attach a directly bound event handler
3535
$( "#list a" ).on( "click", function( event ) {
3636
event.preventDefault();
3737
console.log( $( this ).text() );
@@ -41,7 +41,7 @@ $( "#list a" ).on( "click", function( event ) {
4141
While this works perfectly fine, there are drawbacks. Consider what happens when we add a new anchor after having already bound the above listener:
4242

4343
```
44-
// add a new element on to our existing list
44+
// Add a new element on to our existing list
4545
$( "#list" ).append( "<li><a href='http://newdomain.com'>Item #5</a></li>" );
4646
```
4747

@@ -61,10 +61,10 @@ Understanding how events propagate is an important factor in being able to lever
6161

6262
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*.
6363

64-
Since we know how events bubble, we can create a *delegated* event:
64+
Since we know how events bubble, we can create a *delegated* event:
6565

6666
```
67-
// attach a delegated event
67+
// Attach a delegated event handler
6868
$( "#list" ).on( "click", "a", function( event ) {
6969
event.preventDefault();
7070
console.log( $( this ).text() );
@@ -75,10 +75,10 @@ Notice how we have moved the `a` part from the selector to the second parameter
7575

7676
### Using the Triggering Element
7777

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")?
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")?
7979

8080
```
81-
// attach a delegated event
81+
// Attach a delegated event handler
8282
$( "#list" ).on( "click", "a", function( event ) {
8383
var elem = $( this );
8484
if ( elem.is( "[href^='http']" ) ) {
@@ -92,11 +92,12 @@ This simply passes the `.is()` method a selector to see if the `href` attribute
9292
We can actually simplify our code by allowing the selector parameter of `.on()` do our logic for us:
9393

9494
```
95-
// attach a delegated event with a more refined selector
95+
// Attach a delegated event handler with a more refined selector
9696
$( "#list" ).on( "click", "a[href^='http']", function( event ) {
9797
$( this ).attr( "target", "_blank" );
9898
});
9999
```
100100

101-
##Summary
101+
## Summary
102+
102103
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

Comments
 (0)