Namespacing events in jquery

Events can be namespaced in jquery so that triggering and unbinding them can done in an easier manner.  Generally , if we bind an event handler to a specific event  , we would do it as under:-

$(selector).bind(“click”, eventHandler);

In this case , if we unbind the click event using

$(selector).unbind(“click”), the particular event would be unbound from all controls having the particular selector.

However, if we use namespace while binding events , we can unbind and trigger the events associated with the namespace and ensure that controls having same events bound with different namespace are not affected.

Following is the code snippet :-

 

$(‘.class’).bind(‘click.namespace’, function(){});

$(‘.class’).trigger(‘click.namespace’); // Will trigger

$(‘.class’).trigger(‘click’);           // Will trigger

$(‘.class’).trigger(‘click.other’);     // Won’t trigger

 

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to Namespacing events in jquery

  1. Pingback: Namespacing events in jquery (via Kingshuk20′s Blog) « Haseeb Akhtar

  2. Pingback: Namespacing events in jquery (via Kingshuk20′s Blog) | plugged-in :-)

  3. Pingback: Namespacing events in jquery (via Kingshuk20′s Blog) « Assign Labs

  4. Pingback: Namespacing events in jquery (via Kingshuk20′s Blog) « saurabhgoel

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s