Example of $(this) in jQuery:
$(document).ready(function(){
$('#div1').mouseover(function(){
alert($(this).text());
});
});
Example of this in jQuery:
$(document).ready(function(){
$('#div1').mouseover(function(){
alert(this.innerText);
});
});
Explanation of above two examples and difference between them:-
The this pointer in JavaScript refers to the current object in the code. In JavaScript everything is an object, so the scope of this is always changing. this and $(this) refers to the same element i.e the current object but the only difference is in their usage, when 'this' is wrapped in $() then it behaves as a jQuery object and all the jQuery methods can be used with it thereby showing power of jQuery but this without $() is an object and cannot use jQuery methods thereby uses the native JavaScript to get the value of div element.
0 Comment(s)