Skip to content

Commit 43961a4

Browse files
committed
Converted code examples for all events articles to github markdown
1 parent 5160f6b commit 43961a4

11 files changed

+247
-226
lines changed

page/events/event-delegation.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,31 @@ certain event types were supported. As of jQuery 1.4.2, the `$.fn.delegate`
2525
method is available, and is the preferred method.
2626
</div>
2727

28-
<div class="example" markdown="1">
29-
Event delegation using `$.fn.delegate`
28+
```
29+
//Event delegation using `$.fn.delegate`
3030
3131
$('#myUnorderedList').delegate('li', 'click', function(e) {
3232
var $myListItem = $(this);
3333
// ...
3434
});
35-
</div>
35+
```
3636

37-
<javascript caption="Event delegation using `$.fn.live`">
37+
```
38+
//Event delegation using `$.fn.live`
3839
$('#myUnorderedList li').live('click', function(e) {
3940
var $myListItem = $(this);
4041
// ...
4142
});
42-
</javascript>
43+
```
4344

4445
### Unbinding Delegated Events
4546

4647
If you need to remove delegated events, you can't simply unbind them. Instead,
4748
use `$.fn.undelegate` for events connected with `$.fn.delegate`, and `$.fn.die`
4849
for events connected with `$.fn.live`. As with bind, you can optionally pass
49-
in the name of the bound function.<javascript caption="Unbinding delegated events">
50+
in the name of the bound function.
51+
```
52+
//Unbinding delegated events
5053
$('#myUnorderedList').undelegate('li', 'click');
51-
$('#myUnorderedList li').die('click');</javascript>
52-
<div class="example" markdown="1">
53-
</div>
54+
$('#myUnorderedList li').die('click');
55+
```

page/events/event-extensions.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,23 @@ Note that for all events, the browser's native event object is available in `eve
5151

5252
For example, to set a hook for the "drop" event that copies the "dataTransfer" property, assign an object to jQuery.event.fixHooks.drop:
5353

54-
<javascript>jQuery.event.fixHooks.drop = { props: [ "dataTransfer" ] };
55-
</javascript>
54+
```
55+
jQuery.event.fixHooks.drop = { props: [ "dataTransfer" ] };
56+
```
5657

5758
Since fixHooks are an advanced feature and rarely used externally, we have not added extra code and interfaces to deal with conflict resolution. If there is a chance that some other code may be assigning fixHooks to the same events, the code should check for an existing hook and take appropriate measures. A simple solution might look like this:
5859

59-
<javascript>if ( jQuery.event.fixHooks.drop ) {
60+
```
61+
if ( jQuery.event.fixHooks.drop ) {
6062
throw new Error("Someone else took the jQuery.event.fixHooks.drop hook!");
6163
}
6264
jQuery.event.fixHooks.drop = { props: [ "dataTransfer" ] };
63-
</javascript>
65+
```
6466

6567
When there are known cases of different plugins wanting to attach to the drop hook, this solution might be more appropriate:
6668

67-
<javascript>var existingHook = jQuery.event.fixHooks.drop;
69+
```
70+
var existingHook = jQuery.event.fixHooks.drop;
6871
if ( !existingHook ) {
6972
jQuery.event.fixHooks.drop = { props: [ "dataTransfer" ] };
7073
} else {
@@ -74,7 +77,7 @@ if ( !existingHook ) {
7477
existingHook.props = [ "dataTransfer" ];
7578
}
7679
}
77-
</javascript>
80+
```
7881

7982
### Special event hooks
8083

@@ -91,11 +94,11 @@ When defined, these string properties specify that a special event should be han
9194

9295
The behavior of these properties is easiest to see with an example. Assume a special event defined as follows:
9396

94-
<javascript>jQuery.event.special.pushy = {
97+
```jQuery.event.special.pushy = {
9598
bindType: "click",
9699
delegateType: "click"
97100
};
98-
</javascript>
101+
```
99102

100103
When these properties are defined, the following behavior occurs in the jQuery event system:
101104

@@ -106,7 +109,8 @@ When these properties are defined, the following behavior occurs in the jQuery e
106109

107110
So given the special event above, this code shows that a pushy isn't removed by removing clicks. That might be an effective way to defend against an ill-behaved plugin that didn't namespace its removal of click events, for example:
108111

109-
<javascript>var $p = $("p");
112+
```
113+
var $p = $("p");
110114
$p.on("click", function( e ) {
111115
$("body").append("I am a " + e.type + "!"));
112116
});
@@ -117,16 +121,17 @@ $p.trigger("click"); // triggers both handlers
117121
$p.off("click");
118122
$p.trigger("click"); // still triggers "pushy"
119123
$p.off("pushy");
120-
</javascript>
124+
```
121125

122126
These two properties are often used in conjunction with a `handle` hook function; the hook might, for example, change the event name from "click" to "pushy" before calling event handlers. See below for an example.
123127

124128
#### The handleObj object
125129

126130
Many of the special event hook functions below are passed a `handleObj` object that provides more information about the event, how it was attached, and its current state. This object and its contents should be treated as read-only data, and only the properties below are documented for use by special event handlers. For the discussion below, assume an event is attached with this code:
127131

128-
<javascript>$(".dialog").on("click.myPlugin", "button", {mydata: 42}, myHandler);
129-
</javascript>
132+
```
133+
$(".dialog").on("click.myPlugin", "button", {mydata: 42}, myHandler);
134+
```
130135

131136
type: String
132137
: The type of event, such as `"click"`. When special event mapping is used via bindType or delegateType, this will be the mapped type.
@@ -189,7 +194,8 @@ This `multiclick` special event maps itself into a standard click event, but use
189194

190195
The hook stores the current click count in the data object, so multiclick handlers on different elements don't interfere with each other. It changes the event type to the original "multiclick" type before calling the handler and restores it to the mapped "click" type before returning:
191196

192-
<javascript>jQuery.event.special.multiclick = {
197+
```
198+
jQuery.event.special.multiclick = {
193199
delegateType: "click",
194200
bindType: "click",
195201
handle: function( event ) {
@@ -212,4 +218,4 @@ The hook stores the current click count in the data object, so multiclick handle
212218
$("p").on("multiclick", { clicks: 3 }, function(e){
213219
alert("clicked 3 times");
214220
});
215-
</javascript>
221+
```

page/events/event-helpers.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ will run for `mouseenter`, and the second will run for `mouseleave`.
1515
Prior to jQuery 1.4, the `$.fn.hover` method required two functions.
1616
</div>
1717

18-
<javascript caption="The hover helper function">
18+
```
19+
//The hover helper function
1920
$('#menu li').hover(function() {
2021
$(this).toggleClass('hover');
2122
});
22-
</javascript>
23+
```
2324

2425
### `$.fn.toggle`
2526

@@ -29,8 +30,8 @@ list is called. Generally, `$.fn.toggle` is used with just two functions;
2930
however, it will accept an unlimited number of functions. Be careful, though:
3031
providing a long list of functions can be difficult to debug.
3132

32-
<javascript caption="The toggle helper function">
33-
33+
```
34+
//The toggle helper function
3435
$('p.expander').toggle(
3536
function() {
3637
$(this).prev().addClass('open');
@@ -39,4 +40,4 @@ providing a long list of functions can be difficult to debug.
3940
$(this).prev().removeClass('open');
4041
}
4142
);
42-
</javascript>
43+
```

page/events/events-to-elements.md

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,45 @@ function to multiple events, when you want to provide data to the event hander,
1010
when you are working with custom events, or when you want to pass an object of
1111
multiple events and handlers.
1212

13-
<javascript caption="Event binding using a convenience method">
14-
$('p').click(function() {
15-
console.log('click');
16-
});
17-
</javascript>
18-
19-
<javascript caption="Event biding using the `$.fn.bind` method">
20-
21-
$('p').bind('click', function() {
22-
console.log('click');
23-
});
24-
</javascript>
25-
26-
<javascript caption="Event binding using the `$.fn.bind` method with data">
27-
$('input').bind(
28-
'click change', // bind to multiple events
29-
{ foo : 'bar' }, // pass in data
30-
31-
function(eventObject) {
32-
console.log(eventObject.type, eventObject.data);
33-
// logs event type, then { foo : 'bar' }
34-
}
35-
);
36-
</javascript>
13+
```
14+
//Event binding using a convenience method
15+
$('p').click(function() {
16+
console.log('click');
17+
});
18+
```
19+
20+
```
21+
//Event biding using the `$.fn.bind` method
22+
$('p').bind('click', function() {
23+
console.log('click');
24+
});
25+
```
26+
27+
```
28+
//Event binding using the `$.fn.bind` method with data
29+
$('input').bind(
30+
'click change', // bind to multiple events
31+
{ foo : 'bar' }, // pass in data
32+
function(eventObject) {
33+
console.log(eventObject.type, eventObject.data);
34+
// logs event type, then { foo : 'bar' }
35+
}
36+
);
37+
```
3738

3839
### Connecting Events to Run Only Once
3940

4041
Sometimes you need a particular handler to run only once — after that, you may
4142
want no handler to run, or you may want a different handler to run. jQuery
4243
provides the `$.fn.one` method for this purpose.
4344

44-
<javsacript caption="Switching handlers using the `$.fn.one` method">
45-
$('p').one('click', function() {
46-
console.log('You just clicked this for the first time!');
47-
$(this).click(function() { console.log('You have clicked this before!'); });
48-
});
49-
</javsacript>
45+
```
46+
//Switching handlers using the `$.fn.one` method
47+
$('p').one('click', function() {
48+
console.log('You just clicked this for the first time!');
49+
$(this).click(function() { console.log('You have clicked this before!'); });
50+
});
51+
```
5052

5153
The `$.fn.one` method is especially useful if you need to do some complicated
5254
setup the first time an element is clicked, but not subsequent times.
@@ -58,29 +60,32 @@ the event type to unbind. If you attached a named function to the event, then
5860
you can isolate the unbinding to that named function by passing it as the
5961
second argument.
6062

61-
<javascript caption="Unbinding all click handlers on a selection">
62-
$('p').unbind('click');
63-
</javascript>
63+
```
64+
//Unbinding all click handlers on a selection
65+
$('p').unbind('click');
66+
```
6467

65-
<javascript caption="Unbinding a particular click handler, using a reference to the function">
66-
var foo = function() { console.log('foo'); };
67-
var bar = function() { console.log('bar'); };
68+
```
69+
//Unbinding a particular click handler, using a reference to the function
70+
var foo = function() { console.log('foo'); };
71+
var bar = function() { console.log('bar'); };
6872
69-
$('p').bind('click', foo).bind('click', bar);
70-
$('p').unbind('click', bar); // foo is still bound to the click event
71-
</javascript>
73+
$('p').bind('click', foo).bind('click', bar);
74+
$('p').unbind('click', bar); // foo is still bound to the click event
75+
```
7276

7377
### Namespacing Events
7478

7579
For complex applications and for plugins you share with others, it can be
7680
useful to namespace your events so you don't unintentionally disconnect events
7781
that you didn't or couldn't know about.
7882

79-
<javascript caption="Namespacing events">
80-
$('p').bind('click.myNamespace', function() { /* ... */ });
81-
$('p').unbind('click.myNamespace');
82-
$('p').unbind('.myNamespace'); // unbind all events in the namespace
83-
</javascript>
83+
```
84+
//Namespacing events
85+
$('p').bind('click.myNamespace', function() { /* ... */ });
86+
$('p').unbind('click.myNamespace');
87+
$('p').unbind('.myNamespace'); // unbind all events in the namespace
88+
```
8489

8590
### Binding Multiple Events
8691

@@ -89,12 +94,13 @@ having a different function for handing the event. In these cases you can pass
8994
an object into `$.fn.bind` with one or more key/value pairs, with the key being
9095
the event name and the value being the function to handle the event.
9196

92-
<javascript caption="Binding Multiple Events">
93-
$('p').bind({
94-
'click': function() { console.log('clicked!'); },
95-
'mouseover': function() { console.log('hovered!'); }
96-
});
97-
</javascript>
97+
```
98+
//Binding Multiple Events
99+
$('p').bind({
100+
'click': function() { console.log('clicked!'); },
101+
'mouseover': function() { console.log('hovered!'); }
102+
});
103+
```
98104

99105
<div class="note" markdown="1">
100106
The option to pass an object of multiple events and handlers to `$.fn.bind` was

page/events/inside-event-handling-function.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ the DOM element that the handler was bound to via the keyword this. To turn
4141
the DOM element into a jQuery object that we can use jQuery methods on, we
4242
simply do $(this), often following this idiom:
4343

44-
<javascript>
44+
```
4545
var $this = $(this);
46-
</javascript>
46+
```
4747

48-
<javascript caption="Preventing a link from being followed">
48+
```
49+
//Preventing a link from being followed
4950
$('a').click(function(e) {
5051
var $this = $(this);
5152
if ($this.attr('href').match('evil')) {
5253
e.preventDefault();
5354
$this.addClass('evil');
5455
}
5556
});
56-
</javascript>
57+
```

0 commit comments

Comments
 (0)