adspace


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

How to always reference latest version of jquery?

875


What is the minimum setup needed to start using jquery.

939


How to validate email using jquery?

975


How do you install/use jquery in a project. What is the minimum setup needed to start using jquery.

999


How can jquery library be added to pages?

952


Do you have plans for a new jquery plug-in or something else we should look out for? Where to from here for the imgpreview plugin?

864