jQuery Edge: Bind with a Different “this”

Tuesday, May 12, 2009

Brand new to jQuery SVN is the oft-requested feature of providing a different value for the “this” object in an event callback. Previously jQuery would always send the element as the value of “this” in the callback. You can utilize this new feature by passing the object you’d like to represent “this” in the callback as the last argument to either .bind() or .live(). Lets look at some example code!

The typical use-case for this functionality is that you’ll have an object (or a class) and you want to call a method of that class when you click on a particular element. However, the “this” object inside the method will be set to the element the event happened on. Usually in these situations you’d want the “this” object to represent the instance of your class, not the element. Here is an example that tries to illustrate this use-case.

// Create a class
var MyClass = function() {
    // initialize the class
    this.clicked = false;
    this.element = null;
};
MyClass.prototype.click = function( event ) {
    // handle the click event on an element
    // the "this" object should represent the instance of
    // the class, not the element the event happened on.
    // the element can be retreived using the event.target
    // or event.currentTarget

    // set the clicked property to true
    this.clicked = true;

    // set the element property to the element that was clicked
    this.element = event.target;

    // cancel the event
    return false;
};

// Create an instance of MyClass
var myInstance = new MyClass();

// Bind an event that calls the click method of myInstance
// and make sure the "this" object is set to myInstance
$("li").live("click", myInstance.click, myInstance);

As you can see from the example code the actual element the event happened on can still be retrieved via the event.target or event.currentTarget properties. Both of these properties are normalized/fixed to work cross-browser (as is the event.relatedTarget property). In the example above I used the live method of binding events but the same applies for the bind method.

Posted in jQuery, jQuery Edge with 9 comments

Comments

You could always do this:

$(li).live("click", function(e) {myInstance.click(e)});

By Isaac on Tuesday, May 12, 2009 at 06:19 PM

Is there a difference between event.target and event.currentTarget? Is event.currentTarget equivalent to what is usually referred to as “this”?

By Jörn Zaefferer on Tuesday, May 12, 2009 at 08:24 PM

@Jörn, event.currentTarget represents the element as the event bubbles through the DOM (https://developer.mozilla.org/En/DOM/Event.currentTarget). Yes, the event.currentTarget is the equivalent to the “this” in jQuery event handlers.

By Brandon Aaron on Tuesday, May 12, 2009 at 09:09 PM

@Isaac, The example I used is purposely simplified but your code is currently a common approach to the situation.

By Brandon Aaron on Tuesday, May 12, 2009 at 09:11 PM

Thanks for the clarification Brandon. That implies that event.currentTarget is now properly normalized, too.

Looking forward to using this in my code!

By Jörn Zaefferer on Tuesday, May 12, 2009 at 09:57 PM

so glad this is in? did a name ever get chosen?

By adam j. sontag on Tuesday, May 12, 2009 at 11:17 PM

@adam, I think it is a good addition for those that need it and doesn’t really affect those that don’t. Currently the code uses “thisObject” but the documentation can use whatever. ECMAScript 3.1 uses “thisArg”, Mozilla docs use “thisObject”, some people recommend using “context”. I’m okay with any of those names. :)

By Brandon Aaron on Tuesday, May 12, 2009 at 11:21 PM

Awesome update. What date should this hit the main release?

By Josh Powell on Wednesday, May 13, 2009 at 04:43 PM

@Josh, We don’t have an official date for jQuery 1.3.3 just yet. Unofficially looking to have it released towards the end of this month.

By Brandon Aaron on Wednesday, May 13, 2009 at 05:26 PM

New Comment