Home > Programming > Event Delegation for Prototype

Event Delegation for Prototype

March 23rd, 2009

Today I’d like to present my own rendition of an Event Delegator class based on Prototype. Event Delegation can be used to improve performance and code simplicity by observing one parent element and triggering events on the children when applicable rather than observing each child element individually.

This has the added benefit that new child elements do not require new event observers, and removed child elements do not create memory leaks or require any cleanup. My event delegator class supports both CSS selectors for this purpose as well as binary functions. An example usage scenario is with my ToolTip class. Fancy Javascript tooltips can be added and removed from a page for free, even as the result of an Ajax update by simply adding a recognized CSS class.

This was actually written back in July ’08, but I’d like to post it here now. It could use a little more thorough documentation, but here it is:

Programming ,