Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger events on all elements

Tags:

jquery

I want to use custom events in my J-Query website. Now I'm not sure which one is the best way to trigger a custom event on all elements attached to this event.

An example could be more clarifying here: Basically I could use

$('#id').trigger('customevent');

I don't want the triggering element to have to know all elements attached to the event. So I could use

$('*').trigger('customevent');

which would hopefully do what I want. But I'm afraid that this way has some performance issues.

Another way I was thinking about is using a class. All elements attached to the event could add this class to themselves. Then I can use this class as selector. However, all this smells like workaround.

I really hope that somebody knows an usual way to solve my problem.

like image 304
Sebastian vom Meer Avatar asked Sep 02 '11 22:09

Sebastian vom Meer


1 Answers

Just found the answer by myself (which is now deprecated, see comment below):

$.event.trigger('customevent');
like image 93
Sebastian vom Meer Avatar answered Sep 18 '22 15:09

Sebastian vom Meer