The simplest way - if you use jQuery - use jQuery.trigger (equivalent to publishing) and jQuery.bind (equivalent to subscribing). Also jQuery.unbind (to unsubscribe).
You can use them with DOM events, as well as with custom events.
//Subsribe to my custom Event $(document).bind('MyCustomEvent', function() { alert('Reacting to my custom event ..'); }); // .... somewhere else in the code, publish the event jQuery.event.trigger('MyCustomEvent'); //or $(document).trigger('MyCustomEvent');
[Update from jquery documentation]
(Starting with jQuery 1.7, the .on () and .off () methods are preferred for attaching and removing event handlers on elements.)
source share