views:

82

answers:

4

I'm converting a bunch of hyperlinks to make simple GET requests using jQuery. I want to maintain the reference to this within the Ajax call, do i need to be using bind/live/something else?

$(document).ready(function(){
    $(".mylink").click(function(){
        var url = $(this).attr('href');
        $.get(url,function(data){
            $(this).parent().html(data); // this is now out of scope
        });
        return false;
    });
});
+1  A: 
$(document).ready(function(){
    $(".moderate").click(function(){
        var url = $(this).attr('href');
        var that = $(this);
        $.get(url,function(data){
            that.parent().html(data);
        });
        return false;
    });
});
Balon
note that `this` (and thus `that`) is a DOM node, and `parent()` is a jQuery function. you'd need to do `$(that).parent()`
David Hedlund
that needs to be assigned `$(this)` and not `this`, otherwise you can't call `parent()` on it as it won't be a jQuery object.
Russ Cam
I've corrected it before you wrote you comments guys! :)
Balon
+3  A: 
$(document).ready(function(){
    $(".moderate").click(function(){
        var $this = $(this);
        var url = $this.attr('href');

        $.get(url,function(data){
            $this.parent().html(data);
        });
        return false;
    });
});

That should work for you.

anomareh
+1  A: 

You need to save this to another variable, like this:

$(document).ready(function(){
    $(".mylink").click(function(){
        var realThis = this;
        var url = $(this).attr('href');
        $.get(url,function(data){
            $(realThis).parent().html(data); // realThis is now in scope
        });
        return false;
    });
});

The AJAX callback can access the variables of the outer method, so this technique works fine.

You only need to call live if you want to handle all .mylink elements, even ones that were added later.

SLaks
+1  A: 

Scoping is a mess in javascript :)

In jQuery 1.4, you have a built-in proxy function that can bring the scope into any callback, see: http://api.jquery.com/jQuery.proxy/.

But it's quite easy to create one yourself:

var proxy = function( fn, scope ) {
    if ( typeof fn !== 'function' ) {
        return function() {};
    }
    scope = scope || this;
    return function() {
        return fn.apply( scope, Array.prototype.slice.call(arguments) );
    };
}

$(document).ready(function(){
    $(".moderate").click(function(){
        var url = $(this).attr('href');
        $.get(url, proxy(function(data) {
            $(this).parent().html(data);
        }, this));
        return false;
    });
});

You can also put the scope in a variable and access it later:

$(document).ready(function(){
    $(".moderate").click(function(){
        var scope = this;
        var url = $(this).attr('href');
        $.get(url, function(data) {
            $(scope).parent().html(data);
        });
        return false;
    });
});
David
The general understanding of scoping in Javascript is a mess, not JavaScript scoping per se :)
Russ Cam