Namespace Your Events
A common pattern in jQuery plugin development is the need to undo what the plugin has done. This is usually handled through a method prefixed with "un". Another common pattern is the use of anonymous functions for event handlers. Unbinding events is easy with jQuery but unbinding a single event handler requires the use of a named function. jQuery 1.2 now provides another option for binding and unbinding events: event namespaces.
A Plugin
As a very simple example, let's say I wrote a plugin called "clicked". It provides two methods: clicked and unclicked. The clicked method attaches an event to all matched elements that when clicked adds a class name of "clicked" to the clicked element. The unclicked method removes the "clicked" classes it might have set and the clicked event handlers it attached. Here is the code:
-
(function($){
-
clicked: function() {
-
});
-
},
-
unclicked: function() {
-
}
-
});
-
})(jQuery);
You could use the plugin by calling it like this:
-
$('div').clicked();
And if you wanted to stop using the plugin, you could just call unclicked:
-
$('div').unclicked();
So far, so good.
A Problem
But let's say I'm also using another plugin that attaches a click event to the same elements that the clicked plugin does. Calling unclicked would unbind all the click events, including those bound by your own code or another plugin.
A Solution: Event Namespacing
Event namespacing provides a way to manage specific event handlers. For example, a plugin could namespace its event handlers to make them easier to unbind while still using anonymous functions. To namespace an event, just suffix the event type with a period and a name ("type.namespace").
Here is the clicked plugin again, but this time using event namespaces:
-
(function($){
-
clicked: function() {
-
});
-
},
-
unclicked: function() {
-
}
-
});
-
})(jQuery);
Now it only unbinds the events that it bound in the first place, leaving any other bound events alone.



September 22nd, 2007 at 4:50 am
Please could you have more tutorials on how to integrate jQuery with PHP/MySQL -- perhaps a project, like a simple customer add/edit/delete/search/paging project, which includes auto-complete?
Many thanks.
September 26th, 2007 at 5:04 pm
Great write-up Karl! I actually did my first bit of real-world "event namespacing" the other day when I add a new .unmask() method to the Masked Input plugin. It was a super-simple set of changes but worked wonderfully!
September 26th, 2007 at 5:16 pm
Oooops! I meant Brandon - great job!
September 28th, 2007 at 8:24 am
Thanks for the info here, I was trying to lookup info on the namespaces this morning and couldn't find any on the jQuery website except the reference in the changelog. Keep up the good work with your blog. Pax.
October 14th, 2007 at 5:39 am
Super just what I needed... !!
October 27th, 2007 at 8:49 am
I'm just begin to use JQuery, Thanks for the info!
January 6th, 2008 at 10:32 pm
It's good,
Hope that the exchange links.
Joshua's Blog
http://www.joshua.org.cn
March 1st, 2008 at 11:24 pm
thanks for this great information on Namespace events
June 10th, 2008 at 12:19 am
First I would like to congradulate you on this blog and I have found the writeup useful.
However I have a problem where I bind a click function to the entire document but then want to unbind for a specific element in this case a textarea so that clicks inside the textarea do not trigger the anonymous function. Could using namespace s solve the problem.
I have provided the jQuery code I am using to tdo this below:
June 10th, 2008 at 7:27 pm
Hi Nanda,
Try something like this:
No need for the unbind.