Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery event triggering on all listeners

Tags:

jquery

I have a custom event I've made which I would like to trigger at some point with no relevance to selection.

I.E - I would like to do something that would behave as running

$("*").trigger('customEvent');

But jQuery documentation warns that using the universal selector is very slow. Is there a way to trigger all the object that are bound to a specific event without having to use the universal selector $("*")?

Thanks!

P.S - I'm currently using a specific class called custom_event_listener and use $('.custom_event_listener').trigger('customEvent') to avoid using a universal selector. I'm wondering if there is a way to avoid the use of a class.

like image 215
Ben Avatar asked Jul 28 '11 13:07

Ben


1 Answers

You can trigger an event on everything that has a handler bound like this:

$.event.trigger('customEvent'); 

This loops through $.cache to find what actually has a handler, then fires on those elements...rather than just finding every element and firing the event on each one.

like image 112
Nick Craver Avatar answered Sep 20 '22 16:09

Nick Craver