You can trigger a custom event on any object with trigger(). On the
global document object, for example:

$(document).trigger("myUpdateEvent");

and listen to it like so:

$(document).bind("myUpdateEvent", someFunction);


Fiddling around with the global document object is a bit icky, though.
Better to use a dedicated object that holds your custom events. To let
elements listen to events, you could create a small plugin that takes
a function to execute when the event triggers. Something like:

var eventHolder = {};

$.fn.bindToUpdate = function(f) {
        $(eventHolder).bind("updateEvent", this, function(e) { f.call
(e.data); });
};

$(function() {
        $("#someDiv").bindToUpdate(function() {
                $(this).html("I was updated!");
        });

        $(eventHolder).trigger("updateEvent");
});

Is that what you wanted to do?

On Jan 14, 8:40 am, Tina S <0vcqn5...@gmail.com> wrote:
> would it be possible to setup a global event and connect objects to
> this event?
>
> for example, i have a global "update" event. it gets asynchronously
> triggered.
>
> i would like various elements to be able to listen for this one update
> event; this may be a particular div that wants to update itself based
> on this event, etc.
>
> is this possible?

Reply via email to