1. Code
  2. Web Development

Custom Events, and the Special Events API in jQuery


Web pages, for the most part, are event-driven. Libraries such as jQuery have provided helper methods to make this functionality much easier to grasp. In this tutorial, we'll look at expanding upon these methods to create your own custom namespaced events.

Events in JavaScript

Before the luxury of JavaScript libraries, if you wanted to add a simple click event to an element you needed to do the follow to support all browsers:

Now JavaScript libraries come with helper methods to make event management more digestible. For example, doing the above in jQuery is much more condensed.

Regardless of your implementation, there are three main parts to events:

  • Listener - waits or 'listens' for an event to fire.
  • Dispatcher - triggers the event to fire.
  • Handler - function to be executed when event is fired.

In our click event in the beginning of the tutorial, the listener is the click event waiting for the #myBtn element to be clicked. When the #myBtn element is clicked, it dispatches and will fire the handler; which in this case, is an anonymous function to display the alert() message.

Step 1: Setting up our Page

jQuery allows us to go a step further and create our own custom events. In this tutorial, we'll be using an unordered list of a directory listing and add functionality via custom events that will collapse and expand directories. Let's start with our basic page structure that will be used in the upcoming examples.

Here we are creating a simple directory listing using an unordered list. We've included jQuery from the Google JSAPI CDN and called addIcons(), which adds images of each file and folder depending on the file extension listed. This function is purely for aesthetic purposes. It is not necessary for any of the custom event code we are about to implement. The result of this step and can be viewed here.

Step 2: .bind() and .trigger()

Before we start adding events to our directory listing example, we need to have an understanding of how .bind() and .trigger() work. We use bind() to attach an event to all matched elements that currently reside on the page. Then use .trigger() when you want to dispatch the event. Let's take a look at a quick example.

In the code above, when the element with an id of 'myBtn' is clicked, an alert message will appear. Additionally, our trigger() will actually fire the click event immediately when the page loads. Just keep in mind that bind() is how you attach an event. While .trigger(), you are forcing the event to be dispatched and execute the event's handler.

Step 3: Custom Events using .bind() and .trigger()

The method .bind() isn't just limited to browser events, but can be used to implement your own custom events. Let's begin by creating custom events named collapse and expand for our directory listing example.

First, let's bind a collapse event to all directories represented in our unordered list.

Here we find all elements that are parents and pass the event name collapse into the .bind() method. We've also named the first parameter evt, which represents the jQuery Event object.

Now we select the target of the event and slide up all of its children. Plus, we had a CSS class collapsed to our directory element.

We are chaining events and attaching our expand event at this line.

Just the opposite of our collapse event handler, in the expand event handler we slide down all the children elements of the directory elements and remove the class collapsed from our target element. Putting it all together.

Just this code alone won't do anything for us because the events collapse and expand are unknown and have no idea when to be dispatched. So we add our .trigger() method when we want these events to fire.

If we run this code, our directories will now toggle when clicked between firing the collapse and expand event. But, if you click a nested directory you'll notice our events are actually firing multiple times per a click. This is because of event bubbling.

Event Capture and Bubbling

When you click an element on a page, the event travels, or is captured, from the topmost parent that has an event attached to it to the intended target. It then bubbles from the intented target back up the topmost parent.

For instance, when we click the css/ folder, our event is captured through root/, assets/, and then css/. It then bubbles css/, assets/, then to root/. Therefore, the handler is getting executed three times. We can correct this by adding a simple conditional in the handler for the intended target.

This code will check each current target of the event against the intended target, or currentTarget. When we have a match, only then will the script execute the collapse event. After updating both the collapse and expand event our page will function as expected.

Event Namespacing

A namespace provides context for events. The custom events, collapse and expand, are ambiguous. Adding a namespace to a jQuery custom event is structured event name followed by the namespace. We'll make our namespace called TreeEvent, because our events represent the actions and functionality of a tree folder structure. Once we've added the namespaces to our events, the code will now look like so:

All we needed to change were the event names in the .bind() and .trigger() methods for both the collapse and expand events. We now have a functional example using custom namespaced events.

Note, we can easily remove events from elements by using the unbind() method.

Special Events API

Another way to setup a custom event in jQuery is to leverage the Special Events API. There isn't much documentation on this API, but Brandom Aaron, a core contributor of jQuery, has written two excellent blog posts ( and to help us understand the methods available. Below is a brief explanation of the methods.

  • add - similar to setup, but is called for each event being bound.
  • setup - called when the event is bound.
  • remove - similar to teardown, but is called for each event being unbound.
  • teardown - called when event is unbound.
  • handler - called when event is dispatched.

Now, let's look at how we can combine our custom events into a Special Event that we will call toggleCollapse.

Let's take a look at it section by section.

The first line jQuery.event.special.toggleCollapse creates a new special event called toggleCollapse. We then have our setup method, which iterates over all the namespaces of this event. Once it finds TreeEvent, it binds a click event to the matched elements, which will call jQuery.event.special.toggleCollapse.TreeEvent.handler once the event is fired. Note, we are using a click event as opposed the toggle() function we were using eariler. This is because toggle() is not an event, but an interaction helper function.

Our teardown method is similar to our setup method, but instead we will unbind the click event from all matched elements.

Here we are using the TreeEvent namespace to abstract the handler. In the handler, we toggle between a collapse and expanded state depending if the matched element contains the CSS class "collapsed". Lastly, we set the event type to our the name of our event, toggleCollapse and use the apply() method that will execute the callback argument when we bind this Special Event.

Finally, we bind our Special Event to the directories of our directory listing. Our final result can be viewed here.

Additonal Resources

Below are a few additional resources you might find useful when working with custom events. Thanks for reading!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.