Generic function js

Asked

Viewed 203 times

0

Good afternoon, I have a question related to creating a function for generic use in javascript, the function is this here:

   function calcular() {
      var_quant = 0;
      var valor = document.getElementById("valor").value;
      var quantidade = document.getElementById("quantidade").value;
      var quantidade_devolvida = 
      document.getElementById("quantidade_devolvida").value;


      if (quantidade_devolvida <= quantidade) {
        var var_quant = valor * quantidade_devolvida;
        document.getElementById("var_qnt").value = var_quant;
    }
}

I need to turn this function into a function that works in any input. Please if anyone could help me I really appreciate it.

  • Daniel, is quantity always fixed? It would be interesting to show the structure of these inputs to know what varies and what is fixed. And how you’re calling this function calcular, is by clicking button?

  • It occurs in the input of the final result itself by an onChange, but these values, quantity, quantity_returned and value, come from a json.

2 answers

0

To make a function dynamic, you need to use the values passed by reference within its function.

It would look like this:

function calcular(valor, quantidade, qt_devolvida, resultado) {
      var_quant = 0;
      var valor = document.getElementById(valor).value;
      var quantidade = document.getElementById(quantidade).value;
      var quantidade_devolvida = 
      document.getElementById(qt_devolvida).value;


      if (quantidade_devolvida <= quantidade) {
        var var_quant = valor * quantidade_devolvida;
        document.getElementById(resultado).value = var_quant;
    }
}

let button = document.getElementById('btn');
btn.addEventListener("click", function(){  
  calcular('valor', 'quantidade', 'quantidade_devolvida', 'var_qnt'); //chamada da função passando os ids dos elementos
})
<input type="text" id="valor" placeholder="Valor"/> <br><br>
<input type="text" id="quantidade" placeholder="Quantidade"/> <br><br>
<input type="text" id="quantidade_devolvida" placeholder="Quantidade devolvida"/> <br><br>
<input type="text" id="var_qnt" placeholder="Resultado" disabled/> <br> <br>
<button id="btn"> Calcular </button>

Note: Remember to pass the valores function in the correct order.

  • Sorry guys, this is the first time I use the forum, but what I need is the id to be captured automatically and that these ids pass to the calculation function.

  • @Danielrezende is exactly what is being done in the function. In the call, you put the ids of the elements in order as I mentioned and their values will be automatically transferred to the function. I don’t know what you mean by "automatically capture id".

0

You can use parameters in the function, this way:

function calcular(valor, quantidade, quantidade_devolvida) {
   var var_quant = 0;
   if (quantidade_devolvida <= quantidade) {
     var_quant = valor * quantidade_devolvida;
   }
   return var_quant;
}

And then you can use it as follows:

document.getElementById("var_qnt").value = calcular(
                                          document.getElementById("valor").value,
                                          document.getElementById("quantidade").value,
                                          document.getElementById("quantidade_devolvida").value);

So you can pass inputs from anywhere and centralize output calculation.

EDITION

Or you can use an object as well, to pass the values to the function:

function calcular(objeto) {
   objeto.var_quant = 0;
   if (objeto.quantidade_devolvida <= objeto.quantidade) {
     objeto.var_quant = objeto.valor * objeto.quantidade_devolvida;
   }
}

var seu_objeto = {
    valor : document.getElementById("valor").value,
    quantidade : document.getElementById("quantidade").value,
    quantidade_devolvida : document.getElementById("quantidade_devolvida").value,
    var_quant : 0
};

calcular(seu_objeto);
document.getElementById("var_qnt").value = seu_objeto.var_quant;
  • Thank you my friend, I will test.

  • Unfortunately it didn’t work out

  • At what point did it not work? I made a test here with the first suggestion: http://jsfiddle.net/ynskmf2g/, and with the second suggestion: http://jsfiddle.net/23Ltbxom/, and both worked. Although I did see another error in the code (an extra parenthesis in setting the value of the property quantidade_devolvida), that I just corrected.

  • @Danielrezende Before accepting an answer, test and prove that is what you need.

  • @Pedrogaspar did not understand why "complicate" the function. Not to mention that you are capturing the elements in the function call and within it the values are being captured.

  • @Jorgematheus, when you say "complicate" you’re referring to the second option I gave, using an object? I just wanted to show another way of doing, passing only one parameter. And, I didn’t understand this part of your comment "Not to mention that you are capturing the elements in the function call and within it the values are being captured".

Show 1 more comment

Browser other questions tagged

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