Jquery : Difference between $(this) and this in jquery
this and $(this) refers to the same element. The only difference is the way they are used
‘this’ is wrapped in $() then it becomes a jQuery object
let example to understand:
$(document).ready(function(){ $('#spnValue').mouseover(function(){ alert($(this).text()); }); }); $(document).ready(function(){ $('#spnValue').mouseover(function(){ alert(this.innerText); }); });
‘this’ is used in dom sense, when ‘this’ is wrapped in $() then it becomes a jQuery object and you are able to use the jQuery library.
In the second example, I have to use innerText() to show the text of the span element as this keyword is not a jQuery object yet. So I have to use the native JavaScript to get the value of span element. But once it is wrapped in $() then jQuery method text() is used to get the text of Span.
Share