Edit
The answers indicated that the this keyword is used in jQuery in the same way as in any JavaScript code. That is, the object method receives the object itself as this , this is what happens with the $.fn functions (they are called in jQuery objects). The event handler functions are callback functions, they are not object methods, and it is the caller who decides that this will be referenced inside the function. It usually refers to a DOM element.
Original question
The difference between this and $(this) often explained as how this refers to a DOM object, and $(this) refers to a jQuery object (a DOM element with a jQuery wrapper around it).
In the following example, the handler function is passed this as a DOM element, and wrapping it in $() , we create a jQuery object from it, so we can use functions that live in the $.fn namespace on it.
$('div').on('click', function(event){ event.preventDefault(); $(this).css('backgroundColor', 'red'); });
However, I just came across this explanation on learn.jquery.com:
Plugins | JQuery Training Center
$.fn.greenify = function() { this.css( "color", "green" ); }; $("a").greenify();
Does this mean that this refers to the DOM object when passing the function of the event handler, but refers to the jQuery object when passing to the jQuery object?
Actually, this makes sense, since the method is called for the jQuery object, so it is logical to pass the jQuery object to it.
user1563285
source share