Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between “this”, “$this” and “$(this)”?

What is the difference between these three forms:

this $this $(this) 
like image 445
oshirowanen Avatar asked Oct 08 '10 10:10

oshirowanen


People also ask

When we use $( this code?

Explanation: The $(this) selector is used to select current HTML elements. 19.

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.

Why this is used in JavaScript?

In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.

How this works in jQuery?

The this is a simple javascript (DOM) object, $(this) will turn the object into a jQuery object. var myHeaderDiv = document. getElementById('header'); $myHeaderDiv = $(myheaderDiv); //just a variable transformed into jQuery object, as with this.


1 Answers

In typical usage you'll usually see them like this (the $this usage may vary):

  • this - Refers to the DOM element in the handler you're currently on, but this may be another object entirely in other situations, but it's always the context.
  • $this - Usually created by var $this = $(this) a cached version of the jQuery wrapped version for efficiency (or chain off $(this) to get the same in many cases).
  • $(this) - The jQuery wrapped version of the element, so you have access to all its methods (the ones in $.fn specifically).
like image 125
Nick Craver Avatar answered Sep 18 '22 15:09

Nick Craver