can.delegate

  • function
 

Listen for events from the children of an element.

can.delegate.call(element, selector, eventName, handler)

Parameters

  1. element {HTMLElement}

    The HTML element to bind to.

  2. selector {String}

    A selector for delegating downward.

  3. eventName {String}

    The name of the event to listen for.

  4. handler {function()}

    The function to execute when the event occurs.

Returns

{Object}

The element.

can.delegate(selector, eventName, handler) binds a delegate handler on an object for a given event. It works on:

  • HTML elements and the window

The idea is that delegate can be used on anything that produces delegate events and it will figure out the appropriate way to bind to it. Typically, can.delegate is only used internally to CanJS; however, if you are making libraries or extensions, use can.delegate to listen to events independent of the underlying library.

Delegate binding to an HTMLElement

// Assuming an HTML body like the following:
// <div id="parent">
//     <div class="child">Hello</div>
// </div>

var el = document.getElementById('parent');
can.delegate.call(el, ".child", "click", function(ev) {
    return this; //-> el
});