Difficulty in passing parameters and arguments in callback

1

I have 2 functions, where I want, when satisfying a condition in the main function I must call an external function, using this external function the current parameter in the first function.

//1 função
function check_fields(element)
{   if(this.value === "")
    {   ...
    }else if(!filter.test(this.value))
    {   ...
    }else if(this.id === "input_nome")
    {   input_nome_Ajax(this);//<<-- Aqui quero chamar a 2º função
    }
    console.log(this.id);
}
//2º função
function input_nome_Ajax()
{   ...
    var xmlreq = new XMLHttpRequest();
    xmlreq.open("GET","functions/db/select_ajax_form_criar_conta.php?input_nome=" + this.value,false);//<<---Aqui o "this" está 'undefined' ???
    xmlreq.send(null);//<<--Veja o final da linha acima ^^^^^^
    ...
}

The error is that the value I need in the second function is undefined ...

    
asked by anonymous 18.07.2017 / 01:44

1 answer

3

The this is internal to the first function and passes as argument to the second, so the signature of the second function should be function input_nome_Ajax(value){ and then you can use it like this:

function input_nome_Ajax(value){
    var xmlreq = new XMLHttpRequest();
    xmlreq.open("GET","functions/db/select_ajax_form_criar_conta.php?input_nome=" + value, false);
    ...

// e para chamar:
input_nome_Ajax(this.value);

If you want to call another function and force the same context you can use .apply or .call , and that would be input_nome_Ajax.call(this) . So the function would have the same this as the first one, but I think in this case it would not be necessary.

    
18.07.2017 / 01:46