Difference between $(this) and 'this' in jQuery?



Difference between $(this) and 'this' in jQuery?..

Answer / chaitanya

this and $(this) refers to the same element. The only difference is the way they are used. 'this' is used in traditional sense, when 'this' is wrapped in $() then it becomes a jQuery object and you are able to use the power of jQuery.

Hide Copy Code

$(document).ready(function(){

$('#spnValue').mouseover(function(){

alert($(this).text());

});

});

In below example, this is an object but since it is not wrapped in $(), we can't use jQuery method and use the native JavaScript to get the value of span element.

Hide Copy Code

$(document).ready(function(){

$('#spnValue').mouseover(function(){

alert(this.innerText);

});

});

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More jQuery Interview Questions

How to use css() in jquery?

0 Answers  


What is .empty()? : jquery mobile

0 Answers  


What is the difference between $(window).load and $(document).ready?

0 Answers  


Is jquery library used for server scripting or client scripting?

0 Answers  


What were the biggest challenges of getting the 1.7 release out there?

0 Answers  


Tell me can we have multiple document.ready() function on the same page? : jquery mobile

0 Answers  


What is a jquery ?

0 Answers  


Tell me how to select combobox select value and text using jquery?

0 Answers  


Define data paremeter of jquery ajax method?

0 Answers  


How to redirect to another page using jquery?

0 Answers  


How to debug jquery code/ debug jquery?

0 Answers  


Have any of your startup projects failed dismally - if so, why and how did you learn from them?

0 Answers  


Categories