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 |
Differentiate among .empty() vs .remove() vs .detach() in jquery.
How to write browser specific code using jquery? : jquery mobile
Which sign is used as a shortcut for jQuery?
What is a dom in jquery? How can we make sure that dom is ready?
Is it possible to use other client side libraries like MooTools, Prototype along with jQuery?
What is the use of html() method in jquery?
Explain the features of jquery mobile? : jquery mobile
Whether jQuery HTML work for both HTML and XML documents?
Explain the difference between parent() and parents() methods in jquery? : jquery mobile
Explain deferred and promise object in jquery? : jquery mobile
What is slice() method in jquery?
What is jquery.noconflict? Explain