Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
65
rated 0 times [  68] [ 3]  / answers: 1 / hits: 81854  / 14 Years ago, thu, february 24, 2011, 12:00:00

I have a jquery class within a normal class in javascript. Is it possible to access variables in the scope of the parent class from a callback function in the jquery class?



A simple example of what I mean is shown below



var simpleClass = function () {    
this.status = pending;
this.target = jqueryObject;
this.updateStatus = function() {
this.target.fadeOut(fast,function () {
this.status = complete; //this needs to update the parent class
});
};
};


Now in the above example, the callback function tries to access the scope of the jquery object. is there any way to access the status variable in the parent class?


More From » jquery

 Answers
76

You set this to a variable in the parent function and then use it in the inner function.



var simpleClass = function () {         
this.status = pending;
this.target = jqueryObject;

var parent = this;

this.updateStatus = function() {
this.jqueryObject.fadeOut(fast,function () {
parent.status = complete; //this needs to update the parent class
});
};
};

[#93597] Wednesday, February 23, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
margaritakristinak

Total Points: 502
Total Questions: 127
Total Answers: 98

Location: England
Member since Mon, May 17, 2021
3 Years ago
;