Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .trigger() multiple events

Tags:

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?

like image 812
Zach Wolf Avatar asked Aug 07 '12 16:08

Zach Wolf


1 Answers

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"); 
like image 141
hazzik Avatar answered Nov 16 '22 22:11

hazzik