Skip to content

Commit fcd6a10

Browse files
committed
Triggering Event Handlers: Remove hard breaks in prose
1 parent 970ad52 commit fcd6a10

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

page/events/triggering-event-handlers.md

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,24 @@ attribution:
66
- jQuery Fundamentals
77
---
88

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.
1110

1211
## What handlers can be .trigger()'d?
1312

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.
2014

2115
```
2216
<a href="http://learn.jquery.com">Learn jQuery</a>
2317
```
18+
2419
```
2520
// This will not change the current page
2621
$( "a" ).trigger( "click" );
2722
```
2823

2924
## How can I mimic a native browser event, if not `.trigger()`?
3025

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.
3327

3428
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.
3529

@@ -54,11 +48,7 @@ For more information see the [triggerHandler documentation](http://api.jquery.co
5448

5549
## Don't use `.trigger()` simply to execute specific functions
5650

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()`.
6252

6353
```
6454
// Triggering an event handler the right way
@@ -75,5 +65,4 @@ $( "p" ).on( "click", foo );
7565
foo(); // instead of $( "p" ).trigger( "click" )
7666
```
7767

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

Comments
 (0)