You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: page/events/triggering-event-handlers.md
+6-17Lines changed: 6 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -6,30 +6,24 @@ attribution:
6
6
- jQuery Fundamentals
7
7
---
8
8
9
-
jQuery provides a way to trigger the event handlers bound to an element without any user interaction via the
10
-
`.trigger()` method.
9
+
jQuery provides a way to trigger the event handlers bound to an element without any user interaction via the `.trigger()` method.
11
10
12
11
## What handlers can be .trigger()'d?
13
12
14
-
jQuery's event handling system is a layer on top of native browser events. When an event handler is added using
15
-
`.on( "click", function() {...} )`, it can be triggered using jQuery's `.trigger( "click" )` because jQuery stores a
16
-
reference to that handler when it is originally added. Additionally, it will trigger the JavaScript inside the
17
-
`onclick` attribute. The `.trigger()` function cannot be used to mimic native browser events, such as
18
-
clicking on a file input box or an anchor tag. This is because, there is no event handler attached using jQuery's
19
-
event system that corresponds to these events.
13
+
jQuery's event handling system is a layer on top of native browser events. When an event handler is added using `.on( "click", function() {...} )`, it can be triggered using jQuery's `.trigger( "click" )` because jQuery stores a reference to that handler when it is originally added. Additionally, it will trigger the JavaScript inside the `onclick` attribute. The `.trigger()` function cannot be used to mimic native browser events, 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 corresponds to these events.
## How can I mimic a native browser event, if not `.trigger()`?
30
25
31
-
In order to trigger a native browser event, you have to use [document.createEventObject](http://msdn.microsoft.com/en-us/library/ie/ms536390%28v=vs.85%29.aspx) for < IE9 and [document.createEvent](https://developer.mozilla.org/en/DOM/document.createEvent) for all other browsers.
32
-
Using these two APIs, you can programmatically 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.
26
+
In order to trigger a native browser event, you have to use [document.createEventObject](http://msdn.microsoft.com/en-us/library/ie/ms536390%28v=vs.85%29.aspx) for < IE9 and [document.createEvent](https://developer.mozilla.org/en/DOM/document.createEvent) for all other browsers. Using these two APIs, you can programmatically 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.
33
27
34
28
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.
35
29
@@ -54,11 +48,7 @@ For more information see the [triggerHandler documentation](http://api.jquery.co
54
48
55
49
## Don't use `.trigger()` simply to execute specific functions
56
50
57
-
While this method has its uses, it should not be used simply to call a function that was bound as a click
58
-
handler. Instead, you should store the function you want to call in a
59
-
variable, and pass the variable name when you do your binding. Then, you can
60
-
call the function itself whenever you want, without the need for
61
-
`.trigger()`.
51
+
While this method has its uses, it should not be used simply to call a function that was bound as a click handler. Instead, you should store the function you want to call in a variable, and pass the variable name when you do your binding. Then, you can call the function itself whenever you want, without the need for `.trigger()`.
62
52
63
53
```
64
54
// Triggering an event handler the right way
@@ -75,5 +65,4 @@ $( "p" ).on( "click", foo );
75
65
foo(); // instead of $( "p" ).trigger( "click" )
76
66
```
77
67
78
-
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).
79
-
With this technique, `.trigger()` can be used to notify other sections of code that an application specific event has happened.
68
+
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). With this technique, `.trigger()` can be used to notify other sections of code that an application specific event has happened.
0 commit comments