views:

63

answers:

2
$.ajax({url: path_to_file, cache: false, success: function(html_result){
    $("#window_" + this.id + "_cont_buffer").html(html_result);})

Now then. This function call is with in a function of a class. this.id is a property of said class. will this pass the function value of this.id into the string the anonymous function, or will it try to evaluate it when the function actually gets called, thus making no sense.

If this is not going to work how I want it to, can you recommend how I achieve this.

+1  A: 

By default this will be an internal jQuery object. However, you can override that by explicitly specifying context: this as part of the call. Then, this will be the object you're calling it from.

$.ajax({url: path_to_file, context: this, cache: false, success: function(html_result){
    $("#window_" + this.id + "_cont_buffer").html(html_result);})

will do what you want.

Matthew Flaschen
So is my understanding of how `this` works correct?
thecoshman
@thecoshman, if you don't use the `context: this`, `this` will be an internal jQuery object.
Matthew Flaschen
+2  A: 

In the particular case of $.ajax(), this can be specified using the context attribute. So, Matthew's solution gives you the this that is specified in the function that you make the $.ajax function call from.

You can see the jQuery documentation for more information on setting the this for the success callback.

justkt
Oh right, I didn't notice that he added `context:this`. So that is basically forcing the `this` in the call back function to be be the `this` that is my instance making the AJAX request. clever stuff this.
thecoshman