Difficulty in passing parameters and arguments in callback

Asked

Viewed 23 times

1

I have 2 functions, where I wish, to satisfy a condition in the main function I must call an external function, using in 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 2nd function it is undefined...

1 answer

3


The this is internal to the first function and passes as argument to the second, then 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 the .call, and that would be input_nome_Ajax.call(this). So the function would have the same this that the first, but I think that in this case it would not be necessary.

  • Vlw man, the first way didn’t work, but with input_nome_Ajax.call(this), was....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.