Can I increase the scope of 'this' in the following example?

Posted by Stomped on Stack Overflow See other posts from Stack Overflow or by Stomped
Published on 2010-05-04T15:17:26Z Indexed on 2010/05/04 15:18 UTC
Read the original article Hit count: 179

Filed under:

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) {
       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?

© Stack Overflow or respective owner

Related posts about jQuery