Prototype This page is part of the Prototype JavaScript framework documentation.
Overview | (...)

The Prototype Function.bindAsEventListener function returns an instance of the function pre-bound to the function(=method) owner object. The returned function will have the current event object as its argument.

Syntax

Function.bindAsEventListener(object);

Examples

Watch a simple checkbox for click events:

<input type=checkbox id=myChk value=1> Test?
<script>
//declaring the class
var CheckboxWatcher = Class.create();

//defining the rest of the class implementation
CheckboxWatcher.prototype = {

  initialize: function(chkBox, message) {
    this.chkBox = $(chkBox);
    this.message = message;
    //assigning our method to the event

    this.chkBox.onclick = this.showMessage.bindAsEventListener(this);
  },

  showMessage: function(evt) {
    alert(this.message + ' (' + evt.type + ')');
  }
};

var watcher = new CheckboxWatcher('myChk', 'Changed');
</script>

Test?