@@ -115,21 +115,24 @@ Each question asks about a given topic for a specific category, like "*What is a
115
115
116
116
``` JavaScript
117
117
/*
118
- 7. What is an Event Listener ?
119
- /////////////////////////////
118
+ 7. How do you use event listeners with jQuery ?
119
+ //////////////////////////////////////////////
120
120
• An Event Listener listens for a specific event to happen (i.e. submit, click, etc.) and does
121
121
something (i.e. callback function).
122
- • To take advantage of DOM manipulation, you need to be able to alter the DOM when EVENTS happen.
123
122
• In order to update the DOM, you need to 'listen' for specific events happening.
124
123
• For example:
125
- • an app LISTENS when the user submits a form.
126
- • an app LISTENS for when the user inputs a search term.
127
- • an app LISTENS for when the user clicks on an element in the page to launch an animation.
128
-
124
+ • An app LISTENS when the user submits a form.
125
+ • An app LISTENS for when the user inputs a search term.
126
+ • An app LISTENS for when the user clicks on an element in the page to launch an animation.
129
127
• So an EVENT LISTENER has 2 parts:
130
128
1. Specify what event to listen for.
131
- 2. provide a CALLBACK FUNCTION that runs when the event occurs. */
132
-
129
+ 2. provide a CALLBACK FUNCTION that runs when the event occurs.
130
+
131
+ • Use the ".on" method with the event and the callback to implement an event listener.
132
+ • In the example below, the user listens on main (see event delegation) and, on "click"
133
+ (of the only button in this study) runs a callback function that increments the counter
134
+ by one and displays the updated clickCount.
135
+ */
133
136
function handleClicks () {
134
137
let clickCount = 0 ; // Stores click count.
135
138
$ (' .click-counter' ).text (clickCount); // show current click count.
@@ -139,15 +142,15 @@ Each question asks about a given topic for a specific category, like "*What is a
139
142
});
140
143
}
141
144
142
- function setUpEventHandlers3 () {
145
+ function setUpEventHandlers () {
143
146
handleClicks ();
144
147
}
145
148
146
- function initialize3 () {
147
- setUpEventHandlers3 ();
149
+ function initialize () {
150
+ setUpEventHandlers ();
148
151
}
149
152
150
- $ (initialize3 );
153
+ $ (initialize );
151
154
```
152
155
153
156
0 commit comments