Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  16] [ 3]  / answers: 1 / hits: 32176  / 12 Years ago, tue, august 7, 2012, 12:00:00

I'm writing a jQuery plugin and using .on and .trigger as my pub/sub system. However, I want to trigger multiple events in different scenarios.



Is this possible to do as one string, like the .on method?



Goal:



$this.trigger(success next etc);    // doesn't work


Current solution:



$this
.trigger(success)
.trigger(next)
.trigger(etc); // works, triggers all three events


Any suggestions?


More From » jquery

 Answers
26

JQuery itself does not support triggering multiple events, however you could write custom extension method triggerAll



(function($) {
$.fn.extend({
triggerAll: function (events, params) {
var el = this, i, evts = events.split(' ');
for (i = 0; i < evts.length; i += 1) {
el.trigger(evts[i], params);
}
return el;
}
});
})(jQuery);


And call it like following:



$this.triggerAll(success next etc);

[#83785] Monday, August 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gavenmekhio

Total Points: 732
Total Questions: 89
Total Answers: 93

Location: Central African Republic
Member since Mon, Aug 10, 2020
4 Years ago
;