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 |
How do I check if an HTML element is empty using jQuery?
What are the features of jquery used in web applications?
What is the basic requirement to start with the jquery?
Explain the each() function?
How can jquery library be added to pages? Write a basic jquery code?
How can an element be checked if it contains a specific class?
Explain the difference between .js and .min.js? : jquery mobile
What is a filter in jquery? Explain
How to select class in jquery?
Tell me how to use migrate jquery plugin if possible? : jquery mobile
Explain jquery $.ajax() method?
How to concatenate two strings using jquery?