From 5f74e75c0a3707e572c96424c854ca6530e41188 Mon Sep 17 00:00:00 2001 From: addyosmani Date: Fri, 4 May 2012 00:30:35 +0100 Subject: [PATCH] .bind() -> .on() --- entries/trigger.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/entries/trigger.xml b/entries/trigger.xml index 53c38694..93c4d240 100644 --- a/entries/trigger.xml +++ b/entries/trigger.xml @@ -16,21 +16,21 @@ -

Any event handlers attached with .bind() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user:

-
$('#foo').bind('click', function() {
+    

Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user:

+
$('#foo').on('click', function() {
       alert($(this).text());
     });
     $('#foo').trigger('click');

As of jQuery 1.3, .trigger()ed events bubble up the DOM tree; an event handler can stop the bubbling by returning false from the handler or calling the .stopPropagation() method on the event object passed into the event. Although .trigger() simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.

To trigger handlers bound via jQuery without also triggering the native event, use .triggerHandler() instead.

-

When we define a custom event type using the .bind() method, the second argument to .trigger() can become useful. For example, suppose we have bound a handler for the custom event to our element instead of the built-in click event as we did above:

-
$('#foo').bind('custom', function(event, param1, param2) {
+    

When we define a custom event type using the .on() method, the second argument to .trigger() can become useful. For example, suppose we have bound a handler for the custom event to our element instead of the built-in click event as we did above:

+
$('#foo').on('custom', function(event, param1, param2) {
   alert(param1 + "\n" + param2);
 });
 $('#foo').trigger('custom', ['Custom', 'Event']);
 

The event object is always passed as the first parameter to an event handler, but if additional parameters are specified during a .trigger() call, these parameters will be passed along to the handler as well. To pass more than one parameter, use an array as shown here. As of jQuery 1.6.2, a single parameter can be passed without using an array.

-

Note the difference between the extra parameters we're passing here and the eventData parameter to the .bind() method. Both are mechanisms for passing information to an event handler, but the extraParameters argument to .trigger() allows information to be determined at the time the event is triggered, while the eventData argument to .bind() requires the information to be already computed at the time the handler is bound.

+

Note the difference between the extra parameters we're passing here and the eventData parameter to the .on() method. Both are mechanisms for passing information to an event handler, but the extraParameters argument to .trigger() allows information to be determined at the time the event is triggered, while the eventData argument to .on() requires the information to be already computed at the time the handler is bound.

The .trigger() method can be used on jQuery collections that wrap plain JavaScript objects similar to a pub/sub mechanism; any event handlers bound to the object will be called when the event is triggered.

Note: For both plain objects and DOM objects, if a triggered event name matches the name of a property on the object, jQuery will attempt to invoke the property as a method if no event handler calls event.preventDefault(). If this behavior is not desired, use .triggerHandler() instead.