views:

34

answers:

1

I want to do the following...

    $('.showcomments').click(function()
    {
        $(this).parent().hide();
        jQuery.getJSON('comments.json',function($data)
        {
            $(this).parent().append($data['value']) 
                    //this is meant to be the instance of 
                    //$('.showcomments') that has been clicked
        });
    });

the problem is that the callback of getJSON of course did not inherit the this item... but how do I do what I am intending?

+6  A: 

Reference it in a variable:

$('.showcomments').click(function()
{
    var $th = $(this);   // References the clicked .showcomments
    $th.parent().hide();
    jQuery.getJSON('comments.json',function($data)
    {
        $th.parent().append($data['value']); // will reference the correct element
    });
});
patrick dw
My thoughts exactly. Within the callback function of an AJAX request `this` is a reference to the request.
g.d.d.c