Extends the Element native object to include delegations in the addEvent and addEvents methods.
This library recreates in portion the functionality coming in MooTools 2.0 that supports event delegation and mirrors 2.0's syntax. Also note that this library does not support all custom events nor does it support focus and blur events.
Event checking based on the work of Daniel Steigerwald found here originally. License: MIT-style license. Copyright: Copyright (c) 2008 Daniel Steigerwald, daniel.steigerwald.cz
Delegates the methods of an element's children to the parent element for greater efficiency when a filter is provided. Otherwise works normally as addEvent always does.
myElement.addEvent(typeSelector, fn);
$(element).addEvent('click:relay(a)', function(event, clicked){ event.preventDefault(); //don't follow the link alert('you clicked a link!'); //you can reference the element clicked with the second //argument passed to your callback clicked.setStyle('color', '#777'); });
Delegates the events to the parent just as with addEvent above.
myElement.addEvents(events);
myElement.addEvents({ //monitor an element for mouseover mouseover: fn, //but only monitor child links for click 'click:relay(a)':fn2 });
Removes a method from an element as removeEvent always does. Provided here just for clarity.
myElement.removeEvent(type, fn);
var monitor = function(event, element){ alert('you clicked a link!')}; $(element).addEvent('click:relay(a)', monitor); //link clicks are delegated to element //...now we remove the delegation: $(element).removeEvent('click:relay(a)', monitor);
This syntax is exactly the same as removeEvent always works. If your method was used to delegate that event previously, the delegation will be removed.
Removes a series of methods from delegation if the functions were used for delegation or else works as removeEvents always does. Provided here for clarity.
myElement.removeEvents(events, fn);
var monitor = function(){ alert('you clicked or moused over a link!')}; $(element).addEvents({ 'mouseover:relay(a)': monitor, 'click:relay(a)': monitor }); //link clicks are delegated to element //...now we remove the delegation: $(element).removeEvents({ 'mouseover:relay(a)': monitor, 'click:relay(a)': monitor }); //or we could remove all click:relay(a) events $(element).removeEvents('click:relay(a)');
This syntax is exactly the same as removeEvents always works. If your methods were used to delegate those events previously, the delegations will be removed.
Adds delegation when you pass in an object with selector/events pairs, otherwise works normally as element.set({events...}) always does.
//same as MooTools events with set always work: myElement.set({ events: { click: function1, mouseover: function2 } }); //delegation: myElement.set({ events: { 'click:relay(a)': function1, 'mouseover:relay(a)': function2 } }); //mixed myElement.set({ events: { 'click:relay(a)': function1, mouseover: function2 } });
This documentation is released under a Attribution-NonCommercial-ShareAlike 3.0 License.