@@ -9,62 +9,62 @@ level: beginner
9
9
10
10
### Setting Up Event Responses on DOM Elements
11
11
12
- jQuery makes it straightforward to set up event-driven responses on page elements.
13
- These events are often triggered by the end user's interaction with the page,
14
- such as when text is entered into a form element or the mouse pointer is moved.
15
- In some cases, such as the page load and unload events, the browser itself will
12
+ jQuery makes it straightforward to set up event-driven responses on page elements.
13
+ These events are often triggered by the end user's interaction with the page,
14
+ such as when text is entered into a form element or the mouse pointer is moved.
15
+ In some cases, such as the page load and unload events, the browser itself will
16
16
trigger the event.
17
17
18
- jQuery offers convenience methods for most native browser events. These methods —
19
- including ` $.fn.click ` , ` $.fn.focus ` , ` $.fn.blur ` , ` $.fn.change ` , etc. — are shorthand
20
- for jQuery's ` $.fn.on ` method. The on method is useful for binding the same handler
18
+ jQuery offers convenience methods for most native browser events. These methods —
19
+ including ` $.fn.click ` , ` $.fn.focus ` , ` $.fn.blur ` , ` $.fn.change ` , etc. — are shorthand
20
+ for jQuery's ` $.fn.on ` method. The ` on ` method is useful for binding the same handler
21
21
function to multiple events, when you want to provide data to the event hander,
22
22
when you are working with custom events, or when you want to pass an object of
23
23
multiple events and handlers.
24
24
25
25
```
26
26
// Event setup using a convenience method
27
- $('p' ).click(function() {
28
- console.log(' You clicked a paragraph!' );
27
+ $( "p" ).click(function() {
28
+ console.log( " You clicked a paragraph!" );
29
29
});
30
30
```
31
31
32
32
```
33
33
// Equivalent event setup using the `$.fn.on` method
34
- $('p' ).on(' click' , function() {
35
- console.log(' click' );
34
+ $( "p" ).on( " click" , function() {
35
+ console.log( " click" );
36
36
});
37
37
```
38
38
39
39
### Extending Events to New Page Elements
40
40
41
- It is important to note that ` $.fn.on ` can only create event listeners
42
- on elements that exist * at the time you set up the listeners* . Similar elements created
43
- after the event listeners are established will not automatically pick up event behaviors
44
- you've set up previously. For example:
45
-
46
- ```
47
- $(document).ready(function(){
48
- // Sets up click behavior on all button elements with the alert class
49
- // that exist in the DOM when the instruction was executed
50
- $(' button.alert' ).on(' click' , function(){
51
- console.log(' A button with the alert class was clicked!' );
52
- });
53
- // Now create a new button element with the alert class. This button
54
- // was created after the click listeners were applied above, so it
55
- // will not have the same click behavior as its peers
56
- $(' button' ).addClass(' alert' ).appendTo(document.body);
41
+ It is important to note that ` $.fn.on ` can only create event listeners
42
+ on elements that exist * at the time you set up the listeners* . Similar elements created
43
+ after the event listeners are established will not automatically pick up event behaviors
44
+ you've set up previously. For example:
45
+
46
+ ```
47
+ $( document ).ready(function(){
48
+ // Sets up click behavior on all button elements with the alert class
49
+ // that exist in the DOM when the instruction was executed
50
+ $( " button.alert" ).on( " click" , function() {
51
+ console.log( " A button with the alert class was clicked!" );
52
+ });
53
+ // Now create a new button element with the alert class. This button
54
+ // was created after the click listeners were applied above, so it
55
+ // will not have the same click behavior as its peers
56
+ $( " button" ).addClass( " alert" ).appendTo( document.body );
57
57
});
58
58
```
59
59
60
- Consult the article on event delegation to see how to use ` $.fn.on ` so that
60
+ Consult the article on event delegation to see how to use ` $.fn.on ` so that
61
61
event behaviors will be extended to new elements without having to rebind them.
62
62
63
63
### Inside the Event Handler Function
64
64
65
65
Every event handling function receives an event object, which contains many
66
- properties and methods. The event object is most commonly used to prevent the
67
- default action of the event via the preventDefault method. However, the event
66
+ properties and methods. The event object is most commonly used to prevent the
67
+ default action of the event via the ` . preventDefault() ` method. However, the event
68
68
object contains a number of other useful properties and methods, including:
69
69
70
70
#### pageX, pageY
@@ -86,12 +86,12 @@ Any data that was passed in when the event was bound. For example:
86
86
87
87
```
88
88
// Event setup using the `$.fn.on` method with data
89
- $(' input' ).on(
90
- ' change',
91
- {foo : ' bar' }, // associate data with event binding
92
- function(eventObject) {
93
- console.log(' An input value has changed! ' , eventObject.data.foo);
94
- }
89
+ $( " input" ).on(
90
+ " change",
91
+ { foo: " bar" }, // associate data with event binding
92
+ function( eventObject ) {
93
+ console.log(" An input value has changed! " , eventObject.data.foo);
94
+ }
95
95
);
96
96
```
97
97
@@ -116,52 +116,52 @@ Prevent the default action of the event (e.g. following a link).
116
116
Stop the event from bubbling up to other elements.
117
117
118
118
In addition to the event object, the event handling function also has access to
119
- the DOM element that the handler was bound to via the keyword ` this ` . To turn
119
+ the DOM element that the handler was bound to via the keyword ` this ` . To turn
120
120
the DOM element into a jQuery object that we can use jQuery methods on, we
121
- simply do ` $(this) ` , often following this idiom:
121
+ simply do ` $( this ) ` , often following this idiom:
122
122
123
123
```
124
- var $this = $(this);
124
+ var $this = $( this );
125
125
```
126
126
127
127
A fuller example would be:
128
128
129
129
```
130
130
// Preventing a link from being followed
131
- $('a' ).click(function(eventObject) {
132
- var $this = $(this);
133
- if ($this.attr(' href' ).match(/evil/) ) {
134
- eventObject.preventDefault();
135
- $this.addClass(' evil' );
136
- }
131
+ $( "a" ).click(function( eventObject ) {
132
+ var $this = $( this );
133
+ if ( $this.attr( " href" ).match( /evil/ ) ) {
134
+ eventObject.preventDefault();
135
+ $this.addClass( " evil" );
136
+ }
137
137
});
138
138
```
139
139
140
140
### Setting Up Multiple Event Responses
141
141
142
- Quite often elements in your application will be bound to multiple events. If
143
- multiple events are to share the same handling function, you can provide the event types
142
+ Quite often elements in your application will be bound to multiple events. If
143
+ multiple events are to share the same handling function, you can provide the event types
144
144
as a space-separated list to ` $.fn.on ` :
145
145
146
146
```
147
147
// Multiple events, same handler
148
- $(' input' ).on(
149
- ' click change' , // bind listeners for multiple events
150
- function() {
151
- console.log(' An input was clicked or changed!' )
152
- }
148
+ $( " input" ).on(
149
+ " click change" , // bind listeners for multiple events
150
+ function() {
151
+ console.log( " An input was clicked or changed!" )
152
+ }
153
153
);
154
154
```
155
155
156
- When each event has its own handler, you can pass an object into ` $.fn.on ` with one or
157
- more key/value pairs, with the key being the event name and the value being the function
156
+ When each event has its own handler, you can pass an object into ` $.fn.on ` with one or
157
+ more key/value pairs, with the key being the event name and the value being the function
158
158
to handle the event.
159
159
160
160
```
161
161
// Binding multiple events with different handlers
162
- $('p' ).on({
163
- ' click' : function() { console.log(' clicked!' ); },
164
- ' mouseover' : function() { console.log(' hovered!' ); }
162
+ $( "p" ).on({
163
+ " click" : function() { console.log( " clicked!" ); },
164
+ " mouseover" : function() { console.log( " hovered!" ); }
165
165
});
166
166
```
167
167
@@ -173,65 +173,65 @@ that you didn't or couldn't know about.
173
173
174
174
```
175
175
// Namespacing events
176
- $('p' ).on(' click.myNamespace' , function() { /* ... */ });
177
- $('p' ).off(' click.myNamespace' );
178
- $('p' ).off(' .myNamespace' ); // unbind all events in the namespace
176
+ $( "p" ).on( " click.myNamespace" , function() { /* ... */ } );
177
+ $( "p" ).off( " click.myNamespace" );
178
+ $( "p" ).off( " .myNamespace" ); // unbind all events in the namespace
179
179
```
180
180
181
181
### Tearing Down Event Listeners
182
182
183
183
To remove an event listener, you use the ` $.fn.off ` method and pass in
184
- the event type to off. If you attached a named function to the event, then
184
+ the event type to off. If you attached a named function to the event, then
185
185
you can isolate the event tear down to just that named function by passing it as the
186
186
second argument.
187
187
188
188
```
189
189
// Tearing down all click handlers on a selection
190
- $('p' ).off(' click' );
190
+ $( "p" ).off( " click" );
191
191
```
192
192
193
193
```
194
194
// Tearing down a particular click handler, using a reference to the function
195
- var foo = function() { console.log(' foo' ); };
196
- var bar = function() { console.log(' bar' ); };
195
+ var foo = function() { console.log( " foo" ); };
196
+ var bar = function() { console.log( " bar" ); };
197
197
198
- $('p' ).on(' click' , foo).on(' click' , bar);
199
- $('p' ).off(' click' , bar); // foo is still bound to the click event
198
+ $( "p" ).on( " click" , foo ).on( " click" , bar );
199
+ $( "p" ).off( " click" , bar ); // foo is still bound to the click event
200
200
```
201
201
202
202
### Setting Up Events to Run Only Once
203
203
204
204
Sometimes you need a particular handler to run only once — after that, you may
205
- want no handler to run, or you may want a different handler to run. jQuery
205
+ want no handler to run, or you may want a different handler to run. jQuery
206
206
provides the ` $.fn.one ` method for this purpose.
207
207
208
208
```
209
209
// Switching handlers using the `$.fn.one` method
210
- $('p' ).one(' click' , firstClick);
210
+ $( "p" ).one( " click" , firstClick );
211
211
212
- function firstClick(){
213
- console.log(' You just clicked this for the first time!' );
214
- // Now set up the new handler for subsequent clicks;
215
- // omit this step if no further click responses are needed
216
- $( this).click(function() { console.log(' You have clicked this before!' ); });
212
+ function firstClick() {
213
+ console.log( " You just clicked this for the first time!" );
214
+ // Now set up the new handler for subsequent clicks;
215
+ // omit this step if no further click responses are needed
216
+ $( this ).click( function() { console.log( " You have clicked this before!" ); } );
217
217
}
218
218
```
219
219
220
220
Note that in the code snippet above, the ` firstClick ` function will be executed for
221
- the first click on * each* paragraph element rather than the function being removed from
221
+ the first click on * each* paragraph element rather than the function being removed from
222
222
* all* paragraphs when * any* paragraph is clicked for the first time.
223
223
224
224
` $.fn.one ` can also be used to bind multiple events:
225
225
226
226
```
227
227
// Using $.fn.one to bind several events
228
- $(' input[id]' ).one(' focus mouseover keydown' , firstEvent);
228
+ $( " input[id]" ).one( " focus mouseover keydown" , firstEvent);
229
229
230
- function firstEvent(eventObject) {
231
- console.log('A ' + eventObject.type + ' event occurred for the first time on the input with id ' + this.id)
230
+ function firstEvent( eventObject ) {
231
+ console.log( "A " + eventObject.type + " event occurred for the first time on the input with id " + this.id );
232
232
}
233
233
```
234
234
235
- In this case, the ` firstEvent ` function will be executed once * for each event* . For the snippet above, this means
236
- that once an input element gains focus, the handler function will still execute for the first keydown event on that
235
+ In this case, the ` firstEvent ` function will be executed once * for each event* . For the snippet above, this means
236
+ that once an input element gains focus, the handler function will still execute for the first keydown event on that
237
237
element.
0 commit comments