views:

801

answers:

2

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?

+7  A: 

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()
  }
});
kangax
A: 

what about using el.hasClassName instead of e.findElement

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

Maybe more performant?!

denisjacquemin
Why -1, what is wrong? an explication would be nice
denisjacquemin