Skip to content

Instantly share code, notes, and snippets.

@cwardzala
Created May 15, 2012 14:34
Show Gist options
  • Save cwardzala/2702240 to your computer and use it in GitHub Desktop.
Save cwardzala/2702240 to your computer and use it in GitHub Desktop.
multiple events trigger for jquery
/**
* jQuery eventsTrigger plugin
* because jquery supports $(element).on('eventA eventB eventC',func) and $(element).off('eventA eventB eventC')
* but does not support $(element).trigger('eventA eventB eventC')
* this allows you to pass multiple events to be triggered in order
*
* @param {String|Array} Space-separated string or array of events in order of execution.
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(root.jQuery);
}
})(this, function ($) {
$.fn.eventsTrigger = function (events) {
events = (typeof events === 'string') ? events.split(' ') : events;
if ( typeof events === 'object' && events.length === null ) { return this; };
return this.each(function () {
var $element = $(this);
$(events).each(function (i, event) {
$element.trigger(event);
});
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment