12

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.

5
  • Can't you let this event bubble and capture it at a higher level? Commented Jul 28, 2011 at 14:01
  • So, you want to do something to all elements, but not using a select all elements. There's gotta be another way you can narrow the selection down, but we can't know what it is without more info on what you're doing. Commented Jul 28, 2011 at 14:02
  • @Andre I'm not sure what that means. sort of a newbie in jQuery. I'll google 'bubble jQuery events' and see if it helps. Commented Jul 28, 2011 at 14:03
  • @Zirak The only information I have regarding the selection is "People who have bound on 'customEvent'". Is there maybe a selector that uses this data? Commented Jul 28, 2011 at 14:04
  • Ben, I think Nick Craver's answer solves your problem. Anyway, bubbling is how javascript (not jQuery) event propagates. You should read about it, it's very important to know when dealing with events. Cheers. Commented Jul 28, 2011 at 14:47

1 Answer 1

15

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.

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

2 Comments

Best answer so far. If I don't get an answer that narrows it down even further I'll mark this as the answer.
This is undocumented and deprecated: github.com/jquery/jquery/blob/…

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.