Skip to content

Commit ab1617c

Browse files
committed
Merge pull request jquery#109 from johnkpaul/issue_84_triggering_handlers
Added triggering handlers section. Initial work for jquery#84
2 parents b072fb6 + 1ecc6c6 commit ab1617c

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

content/events/triggering-event-handlers.md

+53-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
11
---
22
title : Triggering Event Handlers
33
attribution: jQuery Fundamentals
4+
github: johnkpaul
5+
level: intermediate
46
---
5-
jQuery provides a way to trigger the event handlers bound to an element without
6-
any user interaction via the `$.fn.trigger` method. While this method has its
7-
uses, it should not be used simply to call a function that was bound as a click
7+
#### Triggering Events
8+
jQuery provides a way to trigger the event handlers bound to an element without any user interaction via the `$.fn.trigger` method.
9+
10+
#### What handlers can be trigger()'d
11+
12+
jQuery's event handling system is a layer on top of native browser events.
13+
When an event handler is added using <code>.click(fn)</code> or <code>.on("click",fn)</code>, it can be triggered using jQuery's <code>.trigger("click")</code>
14+
because jQuery stores a reference to that handler when it is originally added. Additionally, it will trigger the javascript inside the "onclick" attribute. The <code>.trigger()</code> function cannot be used to mimic native browser events,
15+
such as clicking on a file input box or an anchor tag. This is because, there is no event handler attached using jQuery's event system that coorespond to these events.
16+
17+
<html caption="Anchor tag example">
18+
<a href="http://learn.jquery.com">Learn jQuery</a>
19+
</html>
20+
21+
<javascript caption="This will not change the current page">
22+
$('a').trigger('click');
23+
</javascript>
24+
25+
#### How can I mimic a native browser event, if not <code>.trigger()</code>?
26+
27+
In order to trigger a native browser event, you have to use [document.createEventObject](http://msdn.microsoft.com/en-us/library/ie/ms536390(v=vs.85).aspx) for < IE9 and [document.createEvent](https://developer.mozilla.org/en/DOM/document.createEvent) for all other browsers.
28+
Using these two APIs, you can programatically create an event that behaves exactly as if someone has actually clicked on a file input box. The default action will happen, and the browse file dialog will display.
29+
30+
The jQuery UI Team created [jquery.simulate.js](https://github.com/eduardolundgren/jquery-simulate/blob/master/jquery.simulate.js) in order to simplify triggering a native browser event for use in their automated testing. Its usage is modeled after jQuery's trigger.
31+
32+
<javascript caption="Triggering a native browser event using the simulate plugin">
33+
$('a').simulate('click');
34+
</javascript>
35+
36+
This will not only trigger the jQuery event handlers, but also follow the link and change the current page.
37+
38+
39+
#### <code>.trigger()</code> vs <code>.triggerHandler()</code>
40+
41+
There are four differences between <code>.trigger()</code> and <code>.triggerHandler()</code>
42+
43+
1. <code>triggerHandler</code> only triggers the event on the first element of a jQuery object.
44+
2. <code>triggerHandler</code> cannot be chained. It returns the value that is returned by the last handler, not a jQuery object.
45+
3. <code>triggerHandler</code> will not cause the default behavior of the event (such as a form submission).
46+
4. Events triggered by <code>triggerHandler</code>, will not bubble up the DOM heirarchy. Only the handlers on the single element will fire.
47+
48+
For more information see the [triggerHandler documentation](http://api.jquery.com/triggerHandler)
49+
50+
#### Don't use <code>.trigger()</code> simply to execute specific functions
51+
52+
While this method has its uses, it should not be used simply to call a function that was bound as a click
853
handler. Instead, you should store the function you want to call in a
954
variable, and pass the variable name when you do your binding. Then, you can
1055
call the function itself whenever you want, without the need for
11-
`$.fn.trigger`.
56+
`$.fn.trigger`.
1257

1358
<javascript caption="Triggering an event handler the right way">
1459
var foo = function(e) {
@@ -23,3 +68,7 @@ call the function itself whenever you want, without the need for
2368

2469
foo(); // instead of $('p').trigger('click')
2570
</javascript>
71+
72+
A more complex architecture can built on top of trigger using the [publish-subscribe pattern](http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) using [jQuery plugins](https://gist.github.com/661855).
73+
With this technique, `$.fn.trigger` can be used to notify other sections of code that an application specific event has happenned.
74+

0 commit comments

Comments
 (0)