Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do people assign $this = $(this) in many jQuery plugins?

I often see this as the first line of a plug-in:

$this = $(this);

Is this just for efficiency, to avoid getting the jQuery object each and every time?

like image 324
T Nguyen Avatar asked Jun 07 '11 07:06

T Nguyen


People also ask

When we should use $( this in jQuery?

The value of this inside a click event is a DOM element (the one that was clicked). Using $(this) converts it to a jQuery object. DOM elements do not have a hide() methods, but jQuery adds that and many other methods you can then call.

What does $( this mean in jQuery?

$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . 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.


1 Answers

To cache the jQuery object and not have to instantiate it every time they require it.

like image 91
Variant Avatar answered Sep 27 '22 22:09

Variant