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 jQuery selectors are executed?
What is the difference between event.PreventDefault and event.stopPropagation?
How does caching helps and how to use caching in jQuery?
Define the types of selectors in jquery?
What are the basic selectors in jQuery?
How do you select an item using css class or id and get the value by use of jquery.
Can you please explain the difference between .js and .min.js? : jquery mobile
Explain the use of the $.fn.bind and $.fn.trigger.
Jquery can be used in what scenarios?
Is there any advantage of using $.ajax() for ajax call against $.get() or $.post()?
How to get the server response from an ajax request using jquery?
What is the difference between "#" and "." selector in JQuery?