Google Closure a'la jQuery live / on event delegation

I need to delegate an event for newly created elements, I need to bind a handler to their creation event. Something like: onCreate

I do not want to bind the event to the element after creation, accessing 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 any way to do this when closing Google? the goal is to attach the event to the creation and not calling the function to attach it.

Can anyone advise? I am new to Closures.

thanks

+4
closures jquery events delegation google-closure
source share
1 answer

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

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

So, here under $(document) you can have a context in which the element must be present, and .on() has three parameters in it:

  • event name / type
  • the element in which the event should be associated
  • the action that should be run on this particular event.
0
source share

All Articles