I'm curious if its possible to create a custom event in jQuery that isn't bound to a DOM element.
I greatly prefer jQuery to YUI but there is one thing in YUI that I like, which is creating custom events and being able to subscribe to them later.
For example, this creates an event that is bound to a variable, not to a DOM element:
var myevent = new YAHOO.util.CustomEvent("mycustomevent");
All of the examples and documentation I have been reading for jQuery require something like:
$('body').bind('mycustomevent', function(){ //do stuff });
You can trigger custom global events like this in jQuery:
jQuery.event.trigger('mycustomevent', [arg1, arg2, arg3]);
These will trigger for any element.
Since jQuery is built around DOM objects, you have to bind your events to DOM objects. You can probably find some way to bind events without an element too (you did), but that's not a supported methodology.
As you wrote in your own answer, you can bind your event to a global DOM object if you don't want to bind it to an individual page element:
$(document).bind('mycustomevent', function (e, arg1, arg2, arg3) { /* ... */ });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With