Google Closure event delegation a'la jQuery live/on
Asked Answered
M

1

4

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

Mcclelland answered 17/7, 2012 at 7:54 Comment(1)
I'm confused that you say, "I do not want to bind the event to the element after the creation" but also, "the goal is to attach the event on creating". When exactly do you want this event bound?Shae
D
0

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.
Drier answered 26/8, 2014 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.