jQuery Custom Events

jQuery custom events are a great feature to deal with complex situations or non trivial situations.
You can create a custom event that reacts to a particular situation that is not defined with jQuery.

You can attach a custom event just like this:


$('#someElement').on('myCustomEvent', handlerForCustomEvent)

and trigger it like this:


$('#someElement').trigger('myCustomEvent');

Note: You can also attach custom events to non DOM elements, like an object of any class for exemple.

You can see a demo of these feature in this jsFiddle.

Advertisements

3 thoughts on “jQuery Custom Events

  1. Pingback: jQuery Custom Events 2 | Nuno Oliveira

  2. Pingback: Understanding Javascript Publish/Sub | Nuno Oliveira

  3. Pingback: Understanding Javascript Publish/Subscription | Nuno Oliveira

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s