Prototype equivalent for jQuery live function

Jakub Arnold picture Jakub Arnold · Sep 26, 2009 · Viewed 10.2k times · Source

I need to bind event listener to all dynamicaly created elements by given css selector.

In jQuery, that would be

$(".foo").live("click", function(e) {
   // bar
});

Is there any equivalent in Prototype for this?

Answer

kangax picture kangax · Sep 26, 2009

This is usually done with Event#findElement:

document.observe('click', function(e, el) {
  if (el = e.findElement('.foo')) {
    // there's your `el`
    // might want to stop event at this point - e.stop()
  }
});