Skip to content

Commit dbdb2c8

Browse files
Markus Amalthea Magnusonajpiano
authored andcommitted
Style and typography fixes and code style adherence in the Events section. Fixes #294.
1 parent 58ac547 commit dbdb2c8

11 files changed

+595
-690
lines changed

page/events.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ customFields:
77
value: "tasks"
88
---
99
jQuery provides simple methods for attaching event handlers to selections.
10-
When an event occurs, the provided function is executed. Inside the function,
10+
When an event occurs, the provided function is executed. Inside the function,
1111
`this` refers to the element that was clicked.
1212

13-
For details on jQuery events, visit the
13+
For details on jQuery events, visit the
1414
[Events documentation on api.jquery.com](http://api.jquery.com/category/events/).
1515

1616
The event handling function can receive an event object. This object can be

page/events/event-basics.md

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,62 @@ level: beginner
99

1010
### Setting Up Event Responses on DOM Elements
1111

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
1616
trigger the event.
1717

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
2121
function to multiple events, when you want to provide data to the event hander,
2222
when you are working with custom events, or when you want to pass an object of
2323
multiple events and handlers.
2424

2525
```
2626
// 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!" );
2929
});
3030
```
3131

3232
```
3333
// 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" );
3636
});
3737
```
3838

3939
### Extending Events to New Page Elements
4040

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 );
5757
});
5858
```
5959

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
6161
event behaviors will be extended to new elements without having to rebind them.
6262

6363
### Inside the Event Handler Function
6464

6565
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
6868
object contains a number of other useful properties and methods, including:
6969

7070
#### pageX, pageY
@@ -86,12 +86,12 @@ Any data that was passed in when the event was bound. For example:
8686

8787
```
8888
// 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+
}
9595
);
9696
```
9797

@@ -116,52 +116,52 @@ Prevent the default action of the event (e.g. following a link).
116116
Stop the event from bubbling up to other elements.
117117

118118
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
120120
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:
122122

123123
```
124-
var $this = $(this);
124+
var $this = $( this );
125125
```
126126

127127
A fuller example would be:
128128

129129
```
130130
// 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+
}
137137
});
138138
```
139139

140140
### Setting Up Multiple Event Responses
141141

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
144144
as a space-separated list to `$.fn.on`:
145145

146146
```
147147
// 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+
}
153153
);
154154
```
155155

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
158158
to handle the event.
159159

160160
```
161161
// 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!" ); }
165165
});
166166
```
167167

@@ -173,65 +173,65 @@ that you didn't or couldn't know about.
173173

174174
```
175175
// 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
179179
```
180180

181181
### Tearing Down Event Listeners
182182

183183
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
185185
you can isolate the event tear down to just that named function by passing it as the
186186
second argument.
187187

188188
```
189189
// Tearing down all click handlers on a selection
190-
$('p').off('click');
190+
$( "p" ).off( "click" );
191191
```
192192

193193
```
194194
// 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" ); };
197197
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
200200
```
201201

202202
### Setting Up Events to Run Only Once
203203

204204
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
206206
provides the `$.fn.one` method for this purpose.
207207

208208
```
209209
// Switching handlers using the `$.fn.one` method
210-
$('p').one('click', firstClick);
210+
$( "p" ).one( "click", firstClick );
211211
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!" ); } );
217217
}
218218
```
219219

220220
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
222222
*all* paragraphs when *any* paragraph is clicked for the first time.
223223

224224
`$.fn.one` can also be used to bind multiple events:
225225

226226
```
227227
// Using $.fn.one to bind several events
228-
$('input[id]').one('focus mouseover keydown', firstEvent);
228+
$( "input[id]" ).one( "focus mouseover keydown", firstEvent);
229229
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 );
232232
}
233233
```
234234

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
237237
element.

0 commit comments

Comments
 (0)