can.on

  • function
 

Listen for events on an object.

can.on.call(target, eventName, handler)

Parameters

  1. target {Object}

    The object that emits events.

  2. eventName {String}

    The name of the event to listen for.

  3. handler {function()}

    The function to execute when the event occurs.

Returns

{Object}

The target.

can.on(eventName, handler) is an alias for can.bind(eventName, handler) and binds a callback handler on an object for a given event. It works on:

  • HTML elements and the window
  • Objects
  • Objects with bind / unbind methods

The idea is that can.on can be used on anything that produces events and it will figure out the appropriate way to bind to it.

Binding to an object

var obj = {};
can.on.call(obj,"something", function(ev, arg1, arg){
    arg1 // 1
    arg2 // 2
})
can.trigger(obj,"something",[1,2])

Binding to an HTMLElement

var el = document.getElementById('foo')
can.on.call(el, "click", function(ev){
    this // el
});