With jQuery .on()
you can pass an optional parameter to set the event data. Can you do this with trigger as well?
The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.
bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.
If you just need to trigger a click event, you can omit the line that begins with for( . @Parag: Read it again. The loop is to click the same link 50 times, which is what it does.
Can trigger() pass data to your event handlers? Yes (as additional parameters)
Can trigger() pass data into the event.data object directly? No (only on() does this)
// Using this will pass myData to every event handler as the second parameter. trigger('myEvent', [myData]) // Instead of this on('myEvent', function(evt) {...}); // You would do this on('myEvent', function(evt, myData) {...});
The trigger() method does 5 main things.
Numbers 3 and 5 are most important and relevant to you. Since you implicitly define the api for handling this event, you want to be consistent with how you trigger events so that people who use your code can be consistent with how they use it.
Example 1 Consistency
function Car(speed, tires, brakes) { this.speed = speed; this.tires = tires; this.brakes = brakes; } Car.prototype.brake = function(amount) { // You can do this (Event handler will have access to these parameters) car.trigger('brake.car', [this.speed, this.brakes, this.tires, amount]) // Or this (Event handler will have access to these parameters) car.trigger('brake.car', [this, amount]) // but try not to mix and match with the same event type } ... //This is the first way from above (choose one or the other, but don't mix and match). passenger.on('brake.car', {person: passenger}, function(evt, carSpeed, carBrakes, carTires, brakeAmount){ if(brakeAmount > 50) passenger.hangOnTight(); } }) ... // This is the second way from above (choose one or the other, but don't mix and match). passenger.on('brake.car', function(evt, car, brakeAmount){ if(brakeAmount > 50) passenger.hangOnTight(); } })
Example 2 Here is the typical example showing both trigger() and on():
jQuery(document).on('eventName' {eventData1: 'foo', eventData2: 'bar'}, function (evt, extraParam1, extraParam2) { //This code runs when the event is triggered console.log(evt.data.eventData1) // foo console.log(evt.data.eventData2) // bar console.log(extraParam1) // 'extra param 1' console.log(extraParam2) // 'extra param 2' }); jQuery(document).trigger('eventName', ['extra param 1', 'extra param 2']);
So just remember.
.on(): event is bubbling up the dom. do some stuff, add to or use event data and use the extra params that trigger added or not.
Tangent: If you want to define event handlers for dynamic elements that can be added or removed arbitrarily, this is very easy with jQuery. See this answer: In jQuery, how to attach events to dynamic html elements?
I hope I didn't get you wrong but do you mean passing additional data with the trigger
method?
$(app.Model).trigger("foo", additionalData);
And somewhere else...
$(app.Model).on("foo", callback); var callback = function(event, additionalData) { console.log(additionalData); }
Note that if you pass additional data with trigger
, your first parameter in the callback function always is the actual event you are triggering.
The app.Model
I used in the parenthesis is the object that should trigger an event and that also listens on that event. Think of it as kind of a namespace. You can always use document
, any DOM selector or even object you like, just make sure that both the trigger
and the on
must use the same object (that is, DOM elements that are removed from the DOM temporarily are error-prone).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With