Take the values of two inputs, perform the calculation and display a third input

Asked

Viewed 912 times

1

Guys, I’m developing a form that will calculate the value of the freight and discount the debts to reach the net amount that the Driver has to receive. But I wanted some fields to display the values as the user fills in the form.

For example the image, the "Weight Difference" field will calculate the "Output Weight" - "Arrival Weight" to know the difference. Does anyone know of any Javascript/jQuery tutorial that performs something similar that I can base on?

 <script type="text/javascript">
            var tPesoSaida   = document.getElementById( 'ps' );
            var tPesoChegada = document.getElementById( 'pc' );
            var tPesoTotal   = document.getElementById( 'pt' );

            tPesoSaida.onkeyup=calcula;
            tPesoChegada.onkeyup=calcula;

            function calcula() {
                tPesoTotal.value = (tPesoSaida.value - tPesoChegada.value);
            }
        </script>
        <div class="small-2 large-4 columns">
            <label>Peso de Saída</label>
            <input type="text" value="0" placeholder="37000" name="PesoSaida" id="ps"/>
        </div>
        <div class="small-2 large-4 columns">
            <label>Peso de Chegada</label>
            <input type="text" value="0" placeholder="37090" name="PesoChegada" id="pc"/>
        </div>
        <div class="small-2 large-4 columns">
            <label>Diferença de Peso</label>
            <input type="text" value="0" name="PesoTotal"  id="pt" readonly="readonly" tabindex="-1"/>
        </div>
  • And what have you tried to do so far? What is your specific difficulty?

  • I’ve been researching, but I couldn’t find anything related to jQuery that is like a "get out of the field" to perform the event.

  • With this you can calculate while the user is typing... http://stackoverflow.com/questions/1481152/how-to-detect-a-textboxs-content-has-changed

  • But how will I tell who is input1, input2 and input3? To pass the values.

  • Id, field name... any valid selector.

  • Are you really wrong the way I asked the question now? @gmsantos

  • @Julianobazzi if you put the snippet of the HTML that has the fields, help.

  • @Bacco edited the question with the source code of the form. Thank you.

Show 3 more comments

1 answer

3


Here’s a simple demonstration of how to do it in JS:

var tQtd = document.getElementById( 'qtd' );
var tVlr = document.getElementById( 'vlr' );
var tTot = document.getElementById( 'tot' );

tVlr.onkeyup=calcula;
tQtd.onkeyup=calcula;

function calcula() {
  tTot.value = tQtd.value * tVlr.value;
}
<input id="qtd" value="0" name="quantidade"><br>
<input id="vlr" value="0" name="valor"><br>
<input id="tot" value="0" name="total" readonly>

Note that there is no verification of the values, I just put the basic to demonstrate how to do.

If you need more advanced things, like controlling the decimal separator, and adjusting the numerical formatting, we have answers explaining how to do it right here:

How to calculate currency appearing decimal places in JS?

  • Can using a CSS Framework influence the operation of Javascript? I edited the question with the snippet of the code I made based on your.

  • If you really want to use name, you need an index: var tPesoSaida = document.getElementsByName( 'PesoSaida' )[0]; but I still recommend the ID.

  • The Onkeyup in case when the user gives a tab that he will perform?

  • @Julianobazzi when you type anything and release the key. It has to be in Up, because in Down the value of the field is not changed yet. If you prefer to change only when the field loses focus, it is onchange. If you run the demo above, you’ll see it changes when you’re typing.

  • If you want to cover more complex situations (copy and paste, changes by JS, etc.) better a timer updating the field. Instead of calling the function in the key, call in the setinterval.

  • I’m new in this area of Javascript, I’m starting to mess with it now.. I don’t have to give a "set" to code pass the result pro input of the total?

  • @Julianobazzi you clicked on the "run code snippet" above and tested? The function calcula() does just that: calculates and puts in input.

  • I did the test and it really works, only in my code that

  • tTot is the field that has the total. We are placing the result in the property value of this field (i.e., effectively changing the value). You need to check if you have not typed wrong. Notice the index I mentioned in the comment about picking by name?

  • Yes, I tidied up the id and in the code that too. But still no changes in the total field.

  • the way to put the code is correct? It is before the same fields?

  • The ideal for almost any JS is to click on Onload, after the page already assembled. If you are not going to do it in Onload, you can leave this JS at the end of the body, after all the other elements.

  • Thank you very much, it worked.

Show 8 more comments

Browser other questions tagged

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