Sunday, 21 October 2018

Why does my function return undefined whilst I do give it an argument

This is how my code looks like (completely)

function getPassword() {
    let pwd = ($('<input/>', {
   //unrelated code
    })).on('blur', function() {
        //more unrelated code
        } else {
            $(this).css({
                'color': 'gray',
                'border': '1px solid green'
            });
            let cValue = $(this).val();
            console.log(cValue);
            let x = getPasswordConfirm(cValue);
        }
    });
    return pwd;

}

function getPasswordConfirm(cValue) {
    let pwd = ($('<input/>', {
        //more unrelated code
    })).on('blur', function() {
        //unrelated code
        } else {
            if ($(this).val() !== cValue) {
                    console.log(cValue);
//this is where it goes wrong, cValue is undefined???
            } else {
                $(this).css({
                    'color': 'gray',
                    'border': '1px solid green'
                });
                console.log(cValue);
            }
        }
    });
    return pwd;
}

the problem I have is: I use cValue as an argument, yet it doesnt get passed on to my function. When I console.log cValue it returns undefined in my getPasswordConfirm() function. To zoom in on the part where I call the function and try to pass the argument to the parameter:

else {
                $(this).css({
                    'color': 'gray',
                    'border': '1px solid green'
                });
                let cValue = $(this).val();
                console.log(cValue); //this works
                let x = getPasswordConfirm('haha');
            }
        });

and this is the part in my function that should evaluate the current value with the parameter cValue:

 else {
                if ($(this).val() !== cValue) {
                        console.log(cValue); //returns undefined
                } else {
                    $(this).css({
                        'color': 'gray',
                        'border': '1px solid green'
                    });
                    console.log(cValue);
                }

but no matter what I use as an argument, it always returns undefined in my getPasswordConfirm(cValue)function. Yes, I do invoke the functions.



from Why does my function return undefined whilst I do give it an argument

No comments:

Post a Comment