Accessing elements in event handling class methods
The goal is to have access to the element and the class instance (this) inside of the event handling class method.
by rpflorence
HTML
<a href="#">one</a><br>
<a href="#">two</a><br>
<a href="#">three</a><br>
<a href="#">four</a><br>
<a href="#">five</a><br>
<a href="#">six</a><br>
<a href="#">seven</a>
JavaScript
var MyClass = new Class({
initialize: function(elements){
this.elements = $$(elements);
this.bound = this.handler.bind(this);
this.elements.each(function(element){
element.addEvent('click', this.bound);
}, this);
},
dummy: 'Dummy text!',
handler: function(event){
var element = event.target;
var text = element.get('text') + ' ... ' + this.dummy;
alert(text);
}
});
new MyClass('a');