jquery "this" binding issue on event handler
Posted
by clyfe
on Stack Overflow
See other posts from Stack Overflow
or by clyfe
Published on 2010-05-19T09:49:04Z
Indexed on
2010/05/19
9:50 UTC
Read the original article
Hit count: 183
In jquery an event hadler's binding is the event generating DOM element (this points to the dom element).
In prototype to change the binding of an event handler one can use the bindAsEventListener function;
How can I access both the instance and the DOM element from a event handler?
Similar to http://stackoverflow.com/questions/117361/how-can-i-bind-an-event-handler-to-an-instance-in-jquery
function Car(){
this.km = 0;
$("#sprint").click(this.drive); //setup event handler
}
// event handler
// in it I need to access both the clicked element
// and the binding object (instance of car)
Car.prototype.drive = function(){
this.km += 10; // i'd like to access the binding (but jq changes it)
this.css({ left: this.km }); // also the element
// NOTE that is inside this function I want to access them not elsewhere
}
var car = new Car();
© Stack Overflow or respective owner