jQuery custom events

Neeraj Singh

By Neeraj Singh

on July 31, 2009

Following code has been tested with jQuery 1.3 .

Javascript is all about interactions. When an event happens then something should happen. For example hover, click, focus, mouseover etc are basic events which are used on a regular basis. jQuery provides a method called bind to bind an action to an event. For example, if we want an alert message when we click on a link then that can be done like this.

1$("a").bind("click", function () {
2  alert("I have been clicked");

jQuery also allows developers to make use of custom events. How it is going to help us, we are going to see it shortly. First let's take a look at a basic calendar application.

This is a simple javascript calendar which primarily does four things: render calendar itself, pull data from API and update calendar with API data, ability to collapse event information and ability to expand event information.

Code looks like this.

1function redrawForMonth(d) {
2  //business logic to render calendar
3  $("#monthly_calendar_umbrella").updateCalendarWithAPIData();
4  $("#monthly_calendar_umbrella").ExpandAllEvents();
7function updateCalendarWithAPIData() {
8  //business logic to get the data from API and appending data to appropriate date cell
11function CollapseAllEvents() {
12  //business logic
15function ExpandAllEvents() {
16  //business logic

In the above case we have four methods. If all the implementation is filled out and helper methods are added then ,in total, we'll have tons of methods.

And slowly, it'll become difficult to see which methods act on the main element monthly_calendar_umbrella.

Let's look at the same functionality if implemented using jQuery custom events.

2  .bind("redrawForMonth", function (e, d) {})
4  .bind("updateCalendarWithAPIData", function (e, d) {})
6  .bind("CollapseAllEvents", function (e) {})
8  .bind("ExpandAllEvents", function (e) {});

First difference, you will notice is how nicely all the actions possible on the main element are laid out. In this case just one look at the code tells me that four actions can be performed on the main element. This was not so obvious from the first version of the code.

In this case, I am binding events such as 'redrawFroMonth' to the element. Obviously 'redrawForMonth' is not a native event such as 'click' or 'submit'. This is what I mean by binding 'custom events' to elements. In this case 'redrawForMonth' is a custom event.

The other thing that is not so obvious is the shift in focus. Traditional javascript programming has been too obsessed with the elements that is clicked or submitted that causes an action. The emphasis has been too much on keeping the code around the element that creates an action. In this case the code has been developed around the element that is being acted upon.

Now, the last part of the discussion is how to trigger custom events. Well, jQuery has a method called trigger .

Note that while binding a function is passed. The first parameter of that function is always an event handler. In the below example I am passing only one parameter even though the function takes two parameters: event handler element and the parameter I defined.

1$("#monthly_calendar_umbrella").trigger("redrawForMonth", new Date());

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.