Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between $(this) and 'this' in jQuery?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 451
    Comment on it

    Example of $(this) in jQuery: 

    $(document).ready(function(){
        $('#div1').mouseover(function(){
           alert($(this).text());
      });
    });

     

    Example of this in jQuery: 

    $(document).ready(function(){
        $('#div1').mouseover(function(){
           alert(this.innerText);
      });
    });

    Explanation of above two examples and difference between them:-

    The this pointer in JavaScript refers to the current object in the code. In JavaScript everything is an object, so the scope of this is always changing. this and $(this) refers to the same element i.e the current object but the only difference is in their usage, when 'this' is wrapped in $() then it behaves as a jQuery object and all the jQuery methods can be used with it thereby showing power of jQuery but this without $() is an object and cannot use jQuery methods thereby uses the native JavaScript to get the value of div element.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: