Skip to content

Commit 8068347

Browse files
committed
Added rest of events chapter
1 parent ee31a27 commit 8068347

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
chapter : events
3+
section : 4
4+
title : Triggering Event Handlers
5+
attribution: jQuery Fundamentals
6+
---
7+
## Increasing Performance with Event Delegation
8+
9+
You'll frequently use jQuery to add new elements to the page, and when you do, you may need to bind events to those new elements — events you already bound to similar elements that were on the page originally.
10+
Instead of repeating your event binding every time you add elements to the page, you can use event delegation.
11+
With event delegation, you bind your event to a container element, and then when the event occurs, you look to see which contained element it occurred on.
12+
If this sounds complicated, luckily jQuery makes it easy with its `$.fn.live` and `$.fn.delegate` methods.
13+
14+
While most people discover event delegation while dealing with elements added to the page later, it has some performance benefits even if you never add more elements to the page.
15+
The time required to bind event handlers to hundreds of individual elements is non-trivial;
16+
if you have a large set of elements, you should consider delegating related events to a container element.
17+
18+
<div class="note" markdown="1">
19+
### Note
20+
21+
The `$.fn.live` method was introduced in jQuery 1.3, and at that time only certain event types were supported.
22+
As of jQuery 1.4.2, the `$.fn.delegate` method is available, and is the preferred method.
23+
</div>
24+
25+
<div class="example" markdown="1">
26+
Event delegation using `$.fn.delegate`
27+
28+
$('#myUnorderedList').delegate('li', 'click', function(e) {
29+
var $myListItem = $(this);
30+
// ...
31+
});
32+
</div>
33+
34+
<div class="example" markdown="1">
35+
Event delegation using `$.fn.live`
36+
37+
$('#myUnorderedList li').live('click', function(e) {
38+
var $myListItem = $(this);
39+
// ...
40+
});
41+
</div>
42+
43+
### Unbinding Delegated Events
44+
45+
If you need to remove delegated events, you can't simply unbind them.
46+
Instead, use `$.fn.undelegate` for events connected with `$.fn.delegate`, and `$.fn.die` for events connected with `$.fn.live`.
47+
As with bind, you can optionally pass in the name of the bound function.
48+
49+
<div class="example" markdown="1">
50+
Unbinding delegated events
51+
52+
$('#myUnorderedList').undelegate('li', 'click');
53+
$('#myUnorderedList li').die('click');
54+
</div>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
chapter : events
3+
section : 6
4+
title : Excercises
5+
attribution: jQuery Fundamentals
6+
---
7+
## Excercises
8+
9+
###Create an Input Hint
10+
11+
Open the file `/exercises/index.html` in your browser. Use the file `/exercises/js/inputHint.js` or work in Firebug.
12+
Your task is to use the text of the label for the search input to create "hint" text for the search input.
13+
The steps are as follows:
14+
15+
1. Set the value of the search input to the text of the label element
16+
17+
2. Add a class of "hint" to the search input
18+
19+
3. Remove the label element
20+
21+
4. Bind a focus event to the search input that removes the hint text and the "hint" class
22+
23+
5. Bind a blur event to the search input that restores the hint text and "hint" class if no search text was entered
24+
25+
What other considerations might there be if you were creating this functionality for a real site?
26+
27+
### Add Tabbed Navigation
28+
29+
Open the file `/exercises/index.html` in your browser. Use the file `/exercises/js/tabs.js`.
30+
Your task is to create tabbed navigation for the two div.module elements.
31+
To accomplish this:
32+
33+
1. Hide all of the modules.
34+
35+
2. Create an unordered list element before the first module.
36+
37+
3. Iterate over the modules using `$.fn.each`. For each module, use the text of the h2 element as the text for a list item that you add to the unordered list element.
38+
39+
4. Bind a click event to the list item that:
40+
41+
* Shows the related module, and hides any other modules
42+
43+
* Adds a class of "current" to the clicked list item
44+
45+
* Removes the class "current" from the other list item
46+
47+
5. Finally, show the first tab.
48+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
chapter : events
3+
section : 5
4+
title : Event Helpers
5+
attribution: jQuery Fundamentals
6+
---
7+
## Event Helpers
8+
9+
jQuery offers two event-related helper functions that save you a few keystrokes.
10+
11+
### `$.fn.hover`
12+
13+
The `$.fn.hover` method lets you pass one or two functions to be run when the `mouseenter` and `mouseleave` events occur on an element. If you pass one function, it will be run for both events; if you pass two functions, the first will run for `mouseenter`, and the second will run for `mouseleave`.
14+
15+
<div class="example" markdown="1">
16+
### Note
17+
18+
Prior to jQuery 1.4, the `$.fn.hover` method required two functions.
19+
</div>
20+
21+
<div class="example" markdown="1">
22+
The hover helper function
23+
24+
$('#menu li').hover(function() {
25+
$(this).toggleClass('hover');
26+
});
27+
</div>
28+
29+
### `$.fn.toggle`
30+
31+
The `$.fn.toggle` method is triggered by the "click" event and accepts two or more functions.
32+
Each time the click event occurs, the next function in the list is called.
33+
Generally, `$.fn.toggle` is used with just two functions; however, it will accept an unlimited number of functions.
34+
Be careful, though: providing a long list of functions can be difficult to debug.
35+
36+
<div class="example" markdown="1">
37+
The toggle helper function
38+
39+
$('p.expander').toggle(
40+
function() {
41+
$(this).prev().addClass('open');
42+
},
43+
function() {
44+
$(this).prev().removeClass('open');
45+
}
46+
);
47+
</div>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
chapter : events
3+
section : 3
4+
title : Inside the Event Handling Function
5+
attribution: jQuery Fundamentals
6+
---
7+
## Inside the Event Handling Function
8+
9+
As mentioned in the overview, the event handling function receives an event object, which contains many properties and methods.
10+
The event object is most commonly used to prevent the default action of the event via the preventDefault method.
11+
However, the event object contains a number of other useful properties and methods, including:
12+
13+
#### pageX, pageY
14+
The mouse position at the time the event occurred, relative to the top left of the page.
15+
16+
#### type
17+
The type of the event (e.g. "click").
18+
19+
#### which
20+
The button or key that was pressed.
21+
22+
#### data
23+
Any data that was passed in when the event was bound.
24+
25+
#### target
26+
The DOM element that initiated the event.
27+
28+
#### preventDefault()
29+
Prevent the default action of the event (e.g. following a link).
30+
31+
#### stopPropagation()
32+
Stop the event from bubbling up to other elements.
33+
34+
In addition to the event object, the event handling function also has access to the DOM element that the handler was bound to via the keyword this.
35+
To turn the DOM element into a jQuery object that we can use jQuery methods on, we simply do $(this), often following this idiom:
36+
37+
<div class="example" markdown="1">
38+
var $this = $(this);
39+
</div>
40+
41+
<div class="example" markdown="1">
42+
Preventing a link from being followed
43+
44+
$('a').click(function(e) {
45+
var $this = $(this);
46+
if ($this.attr('href').match('evil')) {
47+
e.preventDefault();
48+
$this.addClass('evil');
49+
}
50+
});
51+
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
chapter : events
3+
section : 4
4+
title : Triggering Event Handlers
5+
attribution: jQuery Fundamentals
6+
---
7+
## Triggering Event Handlers
8+
9+
jQuery provides a way to trigger the event handlers bound to an element without any user interaction via the `$.fn.trigger` method.
10+
While this method has its uses, it should not be used simply to call a function that was bound as a click handler.
11+
Instead, you should store the function you want to call in a variable, and pass the variable name when you do your binding.
12+
Then, you can call the function itself whenever you want, without the need for `$.fn.trigger`.
13+
14+
<div class="example">
15+
Triggering an event handler the right way
16+
17+
var foo = function(e) {
18+
if (e) {
19+
console.log(e);
20+
} else {
21+
console.log('this didn\'t come from an event!');
22+
}
23+
};
24+
25+
$('p').click(foo);
26+
27+
foo(); // instead of $('p').trigger('click')
28+
</div>

0 commit comments

Comments
 (0)