2

Can I trigger multi events in trigger() or chain them in JQuery!? E.g:

$(element).trigger('event1 event2 event3');

or

$(element).trigger('event1').trigger('event2');

or

$(element).trigger('event1');
$(element).trigger('event2');
1
  • I'm sure the lastest jQuery supports event queuing Commented Feb 19, 2011 at 11:00

2 Answers 2

6

As other answers note, the best built-in method is:

$(element).trigger('event1').trigger('event2');

However, I find that if you're doing this all over the place, a simple plugin cleans it up quite nicely, allowing the space-separated syntax (like other jQuery methods). Here's a quick example of such a plugin:

$.fn.triggerAll = function(events) {
    if(!events) return this; //don't blow up if .triggerAll() without params
    var self = this;         //keep a reference
    $.each(events.split(" "), function(i, e) { self.trigger(e); });
    return this;
};

Then you can call it with space-separated event names, like this:

$(element).triggerAll('event1 event2 event3');

You can test it out here

Sign up to request clarification or add additional context in comments.

Comments

0
$(element).trigger('event1 event2 event3');

is not supported.

(element).trigger('event1').trigger('event2');

is optimal over

$(element).trigger('event1');
$(element).trigger('event2');

as you are not rebuilding the jQuery object containing $(element) each time you trigger an event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.