$(this) in jQuery callbacks? I’ve used them extensively, but never really paid attention to them until after I wrote the posts on what
this is handled in jQuery and how
$(this) is bound to the current element in jQuery event handler callbacks. The findings were not surprising, but rather show that jQuery is a well designed library.
The findings are simple:
this is a reference to the html DOM element that is the source of the event.
$(this) is a jQuery wrapper around that element that enables usage of jQuery methods.
- jQuery calls the callback using
apply() to bind
- Calling jQuery a second time (which is a mistake) on the result of
$(this) returns an new jQuery object based on the same selector as the first one.
Let’s a have a look at how jQuery handles those cases internally.
There’s more to
More on Methods
Last time I finished with a code example with a method that allowed me to receive payments and show my current cash level. In this post I’ll add the same function as a method to another object.
Coming from C#/C++/Java, the usage of the
In C# (and C++ and Java) the
My first assumption was that inside a method the
this keyword would refer to the current object. I was surprised when things didn’t behave as I expected, but it wasn’t