Calling the Javascript function with just one click

Asked

Viewed 92 times

0

I would like to know how to call more than one function with just a "Click".

window.onload = function(){
    var btn = document.getElementById("resposta");
    btn.addEventListener("click",function(){

        var nomedoCliente = parseFloat(document.getElementById("nmCliente").value)
        calcular(nomedoCliente);
    },false);
    function calcular(a){

        var xNmCliente = document.getElementById("nmCliente").value
        var xVidas = document.getElementById("vidas").value
        var xMarca = document.getElementById("marca").value
        var a = 0
        var execucao
        var coringa = 0


        if (xVidas == "vidas1" && xMarca == "marca1" ) {
            a=1
        }
        else if (xVidas == "vidas1" && xMarca == "marca2" ) {
            a=1
        }
        else if (xVidas == "vidas2" && xMarca == "marca1" ) {
            a=4
        }
        else if (xVidas == "vidas3" && xMarca == "marca1") {
            a=3
        }
        else {
            coringa=0

        }


        if(xMarca=="marca1" && a =="3"){
            execucao = 34
        }
        else if (xMarca =="marca2" && a=="1"){
            execucao = 88
        }
        else{
            execucao == 0
        }
    }
}
<p>Quantas vidas temos?</p>
<select id = "vidas">
 <option value="vidas1" >Menos 1000</option>
 <option value="vidas2" >1000 a 5000</option>
 <option value="vidas3" >Mais de 5000</option>
</select>

<p >O quanto nossa marca é importante para a Operadora?</p>
<select id = "marca">
 <option value="marca1" >Pouco Importante</option>
 <option value="marca2" >Importante</option>
 <option value="marca3" >De relevância estratégica</option>
</select></br>

I’d like to get the result of a, as in the other if, to obtain the result of Execucao.

It is possible to do everything in just one function?

That way I put it, nothing happens when I squeeze Resultado. I’ve looked at other topics, but it’s not making much sense.

  • If the code that is here is the same as you have, then this missing element reply

2 answers

2


You can add several listetiner Vent to a button (or any element) with different functions.

btn.addEventListener("click",function(){ 
//seu primeiro codigo aqui
})

btn.addEventListener("click",function(){ 
//seu segundo codigo aqui
})

If you need two functions to communicate, just create two different functions.

        btn.addEventListener("click",function(){ 
        var recebeValorFuncao1 = funcao1();
        //passo o valor recebido da funcao1 para a funcao2
        funcao2(recebeValorFuncao1);    
        });

function funcao1(){
//faz qualquer coisa aqui
return 1;
}

function funcao2(recebeQualquerCoisa){
//usa o valor da funcao1 com a variavel "recebeQualquerCoisa"

return recebeQualquerCoisa + 1;

}
  • Great, but how do I get the answer from the first function for example and use in the second ?

2

I would like to know how to call more than one function with just a "Click".

Simple, call the function as many times as you want. In theory, it would look like this:

btn.addEventListener("click",function(){

    var nomedoCliente = parseFloat(document.getElementById("nmCliente").value)
    calcular(nomedoCliente); // Chamou a função uma vez
    calcular(nomedoCliente); // Chamou a função outra vez!
    calcular(nomedoCliente); // E por aí vai... Pode chamar a função que quiser (e estiver carregada)
}, false);

In practice, you will have to adapt according to your need.

But it doesn’t stop there! Your function is overriding the parameter:

// ...
function calcular(a){       // a foi passado como parâmetro

    var xNmCliente = document.getElementById("nmCliente").value
    var xVidas = document.getElementById("vidas").value
    var xMarca = document.getElementById("marca").value
    var a = 0               // a foi sobrescrito com o valor: 0
    // ...

Not to mention the other mistakes...

Great, but how do I get the answer from the first function for example and use in the second ?

Simply store the function response in one variable and pass this variable as argument in the other function:

resposta = calcular(nomedoCliente);
novaResposta = umaFuncaoQualquer(resposta);

Browser other questions tagged

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