In using a jquery callback, I found that ‘this’ isn’t defined anymore. I’ve found a work around, which is to set ‘this’ to another variable. For example, like so:
function handler(DATA) {
var myThis = this;
$.post(
'file.php',
DATA,
function() {
// THIS where I need access to 'this', but its not available
// unless I've used the 'myThis' trick above
}
);
}
It works like this, but I am always looking for ‘the right way’ or ‘the better way’ to do things.
Is this the best way? or is there another?
This is fine. I do this all the time in my projects, especially with Ajax calls.
But, make sure to put
varbeforemyThis, or else it will be declared in global scope, which you most definitely don’t want.