I’ve got an event handler that was pre-bound to a specific variable (via $.proxy). As a result, when the handler is triggered, this isn’t the normal value, it’s my pre-bound value.
I’d like to recover this using the handler’s event argument, but this doesn’t seem to map directly to event.currentTarget, event.target, or any other event property.
So, I’ve tried digging through the jQuery source, but the event callback stuff is very convoluted, and I can’t figure out exactly what this is getting set to. Does anyone know how simulate a jQuery event handler this using only the event argument?
* * Edit * *
Just to clarify, here’s an example:
var boundThis = {foo: 'bar'}
var handler = $.proxy(function(event) {
// Because of the $.proxy, this === boundThis
// (NOT the normal "this" that jQuery would set)
// In theory event has everything I need to re-create this,
// but I'm having trouble figuring out exactly how
// Here's a naive/non-functional example of what I'm trying to do
jQueryThis = event.target; // If only this worked ...
}, boundThis);
$(someElement).click(handler);
event.currentTargetis typically the value ofthiswith jQuery events. As described in the docs:Demo: http://jsfiddle.net/rhgEB/.
While
event.targetremains as#baz,event.currentTargetreferences the current element being handled; same asthisdoes without aproxy.* * Edit by machineghost * *
Just to save future readers some time, the “magic formula” for generating a
thisequivalent based on an event object (“e“) is: