Following code has been tested with jQuery 1.3 .
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.
Code looks like this.
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
Let’s look at the same functionality if implemented using jQuery custom events.
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.
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.