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 jQuery selectors are executed?

1 Answers  


What is the difference between event.PreventDefault and event.stopPropagation?

1 Answers  


How does caching helps and how to use caching in jQuery?

1 Answers  


Define the types of selectors in jquery?

1 Answers  


What are the basic selectors in jQuery?

1 Answers  


How do you select an item using css class or id and get the value by use of jquery.

1 Answers  


Can you please explain the difference between .js and .min.js? : jquery mobile

1 Answers  


Explain the use of the $.fn.bind and $.fn.trigger.

1 Answers  


Jquery can be used in what scenarios?

1 Answers  


Is there any advantage of using $.ajax() for ajax call against $.get() or $.post()?

1 Answers  


How to get the server response from an ajax request using jquery?

1 Answers  


What is the difference between "#" and "." selector in JQuery?

1 Answers  


Categories