O'Reilly logo

jQuery Essentials by Troy Miles

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Namespacing events

At times, it is necessary to be able to distinguish between different handlers for the same event without using the handler function. When this need arises, jQuery provides the ability to namespace events. To namespace an event, you add a period and the namespace to the name of the event. For example, to give the click event the namespace of alpha, perform this:

$("button").on("click.alpha", handler);

jQuery only allows you to create namespaces that are one level deep. If you add a second namespace, you don't create a second level; instead, you create a second namespace for the same event. Take a look at the following code:

$("button").on("click.alpha.beta", handler);

The preceding code is equivalent to creating two separate namespaces, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required