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 |
Can you please explain the difference between parent() and parents() methods in jquery? : jquery mobile
Explain the difference between jquery-x.x.x.js and jquery.x.x.x min.js?
Explain .on()? : jquery mobile
Is there any difference between body onload() and document.ready() function?
How to divide a page into parts using jquery mobile? : jquery mobile
What are selectors in jQuery and how many types of selectors are there?
What are the methods used to provide effects in jquery?
What is the difference between jquery.size() and jquery.length?
What does dollar sign mean in jquery? : jQuery Mobile
Explain the script build up by jquery?
What is the use of jQuery.data() method?
Write the code to define a requirejs module with its dependencies.