jQuery: Changeset 5990. “Added a new liveQuery/event delegation hybrid method”. Lets you add events that continue to work as new elements are dynamically appended to the DOM, e.g. $(’div p.foo’).live(’click’, fn). Works by adding an event handler to the root document element itself and relying on event bubbling. I have to admit I preferred the earlier proposal of $(’div’).delegate(’p.foo’..), which feels like it should have much better performance—anyone know of a good plugin that supports this?
Ariel Flesler's Listen plugin does just that, and supports focus and blur events, which don't bubble by default.
http://flesler.blogspot.com/2007/10/jquerylisten.h tml
Jörn Zaefferer - 23rd December 2008 15:11 - #
I wonder if you could also do this by listening for DOM mutation events (DOMNodeInserted et al), then re-running the selector and binding onto any newly selected elements (you'd also need to remove events bound previously to deselected elements).
I guess the downside would be lack of IE support, and the performance hit you take by subscribing to the DOM mutation events themselves -- Mozilla reckons it makes DOM modifications 1.5 to 7 times slower, although I didn't experience noticable sluggishness with them on fairly dynamic sites like Google Maps.
phl - 23rd December 2008 16:39 - #
phl: that's more or less how the old jQuery livequery plugin worked. It didn't use DOM mutation events - instead, it monkey-patched jQuery's various methods that modify the DOM. That way, provided you only use jQuery for DOM manipulation and don't use regular DOM methods any changes got picked up by the library.