Take the 2-minute tour ×

I'm trying to listen for a custom event but I would like to effectively 'ignore' the fact that it's namespaced or somehow listen to all namespaces without defining them individually.

$('#test').on('custom', ...);


The reason I want to be able to do this is because I have multiple ui plugins that fire events when they are hidden/shown. These events are mainly used internally but are namespaced so they don't collide with each other. However I would also just like to know when a particular ui element is hidden, independent of its source to perform other cleanup logic.

In the above example, nothing will happen because the trigger events are namespaced. Can I listen to all namespaces with something to the effect of custom.*?

Fiddle demonstrating the problem.



Even something like this would be desirable but still can't get it to work

$('#test').on('custom.global', log);



share|improve this question
I demonstrated a method to do this on this question: stackoverflow.com/questions/10176607/… It might help, might not. –  agrothe Sep 26 '12 at 16:28
add comment

2 Answers

Try triggerAll instead of trigger:

(function($) {
    $.fn.triggerAll = function(topics, data, delimiter) {
        return this.each(function() {
            var $this = $(this), chain = [], t = '';
            delimiter = (delimiter || '.');
            // rebuild chain
            $.each(topics.split(delimiter), function(i,n) {
                t += (i == 0 ? '' : delimiter) + n;

            // append/prepend original topic?
            data = (data || []);
            $.each(chain, function(i,t) {
                $this.trigger(t, data);

Granted, due to how jQuery handles triggering namespacing, triggering the "root" event actually fires the namespaced versions, so to get what you expect you'd need to use another character for the delimiter, like /, and then declare your events like:

var $o = $('#whatever');
// this will be triggered for all events starting with 'root'
$o.on('root', function () { console.log(Array.prototype.slice.call(arguments, 0)); });
// some arbitrary way to fire custom events
$o.on('click', function () {
    $o.triggerAll('root/custom1/subA', ['1st', '2nd'], '/');
    $o.triggerAll('root/custom2', [3, 4, 5], '/');

Example in this fiddle

share|improve this answer
wait a minute...that's not right...needs to build each increasing chain. –  drzaus Feb 21 '13 at 2:45
updated and fixed, now it's more complicated. –  drzaus Feb 21 '13 at 3:38
add comment

I fear the only way would be to list them all as a first argument of on method ...

$('#test').on('custom.ns1 custom.ns2 ...', function(evt){});

Which means annoying code review when the plugin is getting updated ...

share|improve this answer
Unfortunately it seems that that is not the case, see my edit for a fiddle that demonstrates the problem. I also cannot simply use .is(':hidden') because I need to be able to listen for when elements become hidden –  jschr Sep 25 '12 at 20:19
May the events be caught deeper in the DOM tree, can prevented from bubbling :/ ... About the hidden part, this is not really about testing if it is "visibly hidden" :p, but catching an event that would indicate 'the widget got hidden' right ? –  f00bar Sep 25 '12 at 20:28
Sorry, yes this is about watching for when particular modules/widgets become hidden or shown. To be more specific I have a widget for Tooltips that I want to be able to work if they are inside a Tab or Modal. I would like to be able to know when the parent is hidden (which could be either a tab or modal) so that I can hide the tooltips as well. I will have to think about the stopPropagation method some more, it could work but is definitely not ideal. Thanks for the reply. –  jschr Sep 25 '12 at 20:35
@jschr Have you tried to list them all ? I mean all event.namespaces in the first argument of on method ? Cause it works ! –  f00bar Sep 25 '12 at 20:50
This is actually what I'm trying to avoid as it would make maintain quite a pain as new types of widgets are created. I would have to make sure to add the new namespace everywhere that's watching for the hidden event. –  jschr Sep 25 '12 at 20:53
show 2 more comments

Your Answer


