Type: Element

Extends the Element type to include event-delegation in its event system.

Event delegation is a common practice where an event listener is attached to a parent element to monitor its children rather than attach events to every single child element. It's more efficient for dynamic content or highly interactive pages with a lot of DOM elements.



An example of how delegation is usually applied. Delegation is extra useful when working with dynamic content like AJAX.

var myElement = $('myElement');
var request = new Request({
    // other options
    onSuccess: function(text){
        myElement.set('html', text); // No need to attach more click events.
// Adding the event, notice the :relay syntax with the selector that matches the target element inside of myElement.
// Every click on an anchor-tag inside of myElement executes this function.
myElement.addEvent('click:relay(a)', function(event, target){
        url: target.get('href')


  • Delegation is especially useful if you are using AJAX to load content into your pages dynamically, as the contents of an element can change with new elements added or others removed and your delegated events need not change.
  • By delegating events to parent objects you can dramatically increase the efficiency of your pages. Consider the example above. You could attach events to every link on a page - which may be hundreds of DOM elements - or you can delegate the event to the document body, evaluating your code only when the user actually clicks a link (instead of on page load/domready).
  • You can use any selector in combination with any event
  • Be wary of the cost of delegation; for example, mouseover/mouseout delegation on an entire document can cause your page to run the selector constantly as the user moves his or her mouse around the page. Delegation is not always the best solution.
  • In general it is always better to delegate to the closest parent to your elements as possible; delegate to an element in the page rather than the document body for example.

Element method: addEvent

Delegates the methods of an element's children to the parent element for greater efficiency when a selector is provided. Otherwise it will work like addEvent.


myElement.addEvent(typeSelector, fn);


  1. typeSelector - (string) An event name (click, mouseover, etc) that should be monitored paired with a selector (see example) to limit functionality to child elements.
  2. fn - (function) The callback to execute when the event occurs (passed the event just like any other addEvent usage and a second argument, the element that matches your selector that was clicked).


// Alerts when an anchor element with class 'myStyle' is clicked inside of myElement
document.id(myElement).addEvent('click:relay(a.myStyle)', function(){
document.id('myElement').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');


  • element - This element.

Element method: addEvents

Delegates the events to the parent just as with addEvent above. Works like addEvents.


    // monitor an element for mouseover
    mouseover: fn,
    // but only monitor child links for clicks
    'click:relay(a)': fn2

Element method: removeEvent

Removes a method from an element like removeEvent.


var monitor = function(event, element){
    alert('you clicked a link!');
$('myElement').addEvent('click:relay(a)', monitor);
// link clicks are delegated to element
// remove delegation:
$('myElement').removeEvent('click:relay(a)', monitor);

Element method: removeEvents

Removes a series of methods from delegation if the functions were used for delegation or else works like removeEvents.


var monitor = function(){
    alert('you clicked or moused over a link!');
    'mouseover:relay(a)': monitor,
    'click:relay(a)': monitor
// link clicks are delegated to element
// remove the delegation:
    'mouseover:relay(a)': monitor,
    'click:relay(a)': monitor
// remove all click:relay(a) events

Эта документация распостраняется на правах Attribution-NonCommercial-ShareAlike 3.0 License.
Оригинал документации на английском.
© Linux.ria.ua, 2008-2018