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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the goal of cdn and what are the advantages of using cdn?

459


Explain width() vs css(‘width’)?

423


How to use transition effects? : jQuery Mobile

457


How to executed jquery selectors? : jquery mobile

461


Is numeric in jquery?

455






How to revert the most recent ‘destructive’ operation, changing the set of matched elements to its previous state using ?

467


How you get server response from an ajax request using jquery?

449


Define url paremeter of jquery ajax method?

463


How to add or remove classes to an element in jquery?

438


What is the purpose of jquery?

490


Explain the difference between find and children methods in jquery?

461


Explain the common methods of sending a request to a server.

477


What is ajax and how it works?

486


Explain the each() function?

485


How does jquery mobile theming work? : jquery mobile

436