Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts

    $(this). what does it mean

    hi

    in jquery

    what does the "this" mean in $(this).css("background-color","yellow");

    and in what context is it used?

    thanks

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,955
    Thanks
    9
    Thanked 726 Times in 720 Posts
    Here, it's the element on which jQuery is acting. So for example if you assign a click event:

    Code:
    $('#myelement').click(function(){
         $(this).css("background-color","yellow");
    });
    then $(this) would refer to #myelement. This becomes more useful when the selector applies to multiple elements:

    Code:
    $('.myelements').click(function(){
         $(this).css("background-color","yellow");
    });
    when it will apply to whichever element having class myelements was clicked. Another common usage might be within .each():

    Code:
    $('.myelements').each(function(){
         $(this).css("background-color","yellow");
    });
    where it would apply to each element having class myelements in turn through the .each iteration.

    That might do you for starters - for some more detail Google finds this article.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •