Search code examples
jqueryeventsclosuresgoogle-closuredelegation

Google Closure event delegation a'la jQuery live/on


I need to delegate event to newly created elements, I need to attach handler to their creation event. Something similar to: onCreate

I do not want to bind the event to the element after the creation by addressing it: jQuery:

$(element).click(function(){});

I would prefer something like

$.on('document','spawn', '.item', function(e) {
    if (e.target == this.target) {
        alert('element created: '+this);
    }
});

Is there a way to do so in google closure? the goal is to attach the event on creating and not calling function to attach it.

Could anyone advice? I am new to Closures.

Thanks


Solution

  • So we have something similar in jQuery but the method is different. Here is the example:

    $(document).on('spawn', '.item', function(e) {
        if (e.target == this.target) {
            alert('element created: '+this);
        }
    });
    

    So, here under $(document), you can have the context where the element has to present and .on() has three params in it:

    • event name/type
    • the element where event has to bind
    • the action what has to be triggered on that particular event.