I have a doubt in a function js

0

function resta(numr, numr1, resultador) {

    resultador.value = (numr - numr1);
  }


function calculo(cantidad, precio, inputtext, totaltext) {

  gndtotal = totaltext.value - inputtext.value;


  subtotal = (precio * cantidad);
  inputtext.value = subtotal;

  total = eval(gndtotal);
  totaltext.value = total + subtotal;
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr id="panela">
  <td>
    <label class="title">Panela:</label><br>
    <label class="num">5</label>
  </td>

  <td>
    <input type="hidden" id="PanelaIn" name="PanelaIn" value="<?php echo $ver[3];  ?>">

    <input type="number"
      class="form-control form-control-lg" 
      id="Panela" name="Panela" placeholder="Saldo"
      required onChange="resta(PanelaIn.value,this.value,resPanela);"><br>

    <input type="number"
      class="form-control form-control-lg"
      id="resPanela" name="resPanela" 
      onchange="calculo(this.value,preciopanela.value,RPanela,total);">
  </td>
  <input type="hidden" id="preciopanela" value="150">


  <td>
    <input type="text"
      class="form-control form-control-sm"
      id="RPanela" name="RPanela" value="0" readonly>
  </td>
  

I have two functions, subtraction and calculation. each individual function works well.

The problem I have is that the calculation function does not recognize the variable "quantity" that is generated automatically by the subtraction function.

I do not know if it's the best option for this calculation. Thank you very much for your time

    
asked by Jesus Emanuel Becerra Santamar 14.09.2018 в 03:14
source

1 answer

0

You must make a return in the subtraction function, then you create a variable and assign it as a value the call to that function

function resta(numr, numr1, resultador) {

  return resultador.value = (numr - numr1);
}

let cant = resta(tus, parametros, aca)


function calculo(cantidad, precio, inputtext, totaltext) {

  gndtotal = totaltext.value - inputtext.value;


  subtotal = (precio * cantidad);
  inputtext.value = subtotal;

  total = eval(gndtotal);
  totaltext.value = total + subtotal;
}

let resuelto = calculo(cant, tus, parametros, aca)
    
answered by 14.09.2018 в 04:57