0

I can't seem to be able to set this.chkOK inside my ajax function. I'm sorta clueless how I go about doing this so I thought maybe calling validateFields.call(this) should of fixed my problem but I found out that wasn't the case. So I'm sorta lost what to do for my next step. I don't want to set this to a global variable unless I have to. I'm trying to set this.chkOK = true

function validateFields() {

this.chkOK = null;

this.username = function() {
    if(FS.gID('username').value.length >= 2) {

        var user = FS.gID('username').value;


        //Make sure that the username doesn't already exist
        FS.ajax('/server/chkUser.php?user='+user,'GET',function(){
            validateFields.call(this);
            if(xmlText == 0) {

                    this.chkOK = true;
                alert("This user doesn't exist.");


            }
            else if(xmlText == 1) {
                alert("Theres already a user with this username");
                this.chkOK = false;

            }
        });

    }
    else {
        alert("empty");
        this.chkOK = false;
    }
alert(this.chkOK);

}
 }
Jon W
  • 39
  • 6
  • You should format/indent your code, both for your own sake and that of other people looking at it. –  Jan 26 '17 at 04:06

2 Answers2

1

The value of this in your example is NOT the function inside which it's declared, like you assume in your code.

If you simply use var chkOK = null; instead of this.chkOK = null; it should start to work.

jaybee
  • 2,240
  • 14
  • 20
0

It is because this inside the FS.ajax is not the same with this you intended to work with. this in FS.ajax means this of FS

You may assign this into another variable and use it inside FS.ajax. For example,

Note: Unless you know the reason why you put this.chkOk inside the function (such as expected validateFields be invoked by call or apply) this is global object (which you don't want to) or undefined in strict mode which will cause the code to fail

function validateFields() {

    this.chkOK = null;

    // ** assign this to that. So you can reference it inside FS.ajax **
    var that = this;

    this.username = function() {
        if(FS.gID('username').value.length >= 2) {
            var user = FS.gID('username').value;

            //Make sure that the username doesn't already exist
            FS.ajax('/server/chkUser.php?user='+user,'GET',function(){
                validateFields.call(this);
                if(xmlText == 0) {
                    that.chkOK = true; // use `that` instead of `this`. 
                    alert("This user doesn't exist.");
                } else if(xmlText == 1) {
                    alert("Theres already a user with this username");
                    that.chkOK = false; // use `that` instead of `this`
                }
            });
        } else {
            alert("empty");
            this.chkOK = false;
        }

        alert(this.chkOK);
    }
}
gie3d
  • 766
  • 4
  • 8
  • What is `this` in the `this.chkOK = null;` statement at the top? –  Jan 26 '17 at 04:07
  • Honestly I don't know as I don't see the full code... I expect that this function lives in an object that's why he puts this in the code. – gie3d Jan 26 '17 at 04:09
  • A naked reference to `this` inside a function, unless that function is invoked with `call` or `apply`, will be the global object, or in strict mode `undefined`, which will cause his code to fail, so if he means for that to be a variable local to the function, it seems that you should point that out. –  Jan 26 '17 at 04:18
  • Thank you very much for the clarification. – gie3d Jan 26 '17 at 04:23