Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>HTML:</p> <pre><code>&lt;div id="box"&gt; &lt;p&gt; Valor: &lt;input id="valor"&gt; &lt;/p&gt; &lt;p&gt; Taxa: &lt;input id="taxa"&gt; &lt;/p&gt; &lt;p&gt; Imposto: &lt;input id="imposto"&gt; &lt;/p&gt; &lt;p&gt; Envio: &lt;input id="envio"&gt; &lt;/p&gt; &lt;p&gt; Taxa adicional: &lt;input id="taxa_adicional"&gt; &lt;/p&gt; &lt;p&gt; Subtotal: &lt;span id="subtotal"&gt;0&lt;/span&gt; &lt;/p&gt; &lt;p&gt; Total: &lt;span id="total"&gt;0&lt;/span&gt; &lt;/p&gt; &lt;/div&gt; </code></pre> <p>JavaScript:</p> <pre><code>var options = { format: '#,###.00', locale: 'br' }, inputs = $( 'input:text', '#box' ).get(), input_adicional = $( '#taxa_adicional' )[0], input_total = $( '#total' )[0], input_subtotal = $( '#subtotal' )[0]; </code></pre> <p>and then:</p> <pre><code>$( inputs ).add( [ input_total, input_subtotal ] ).format( options ); $( inputs ). blur( function () { var total = 0, subtotal = 0; // on blur, format the field $( this ).format( options ); // calculate the sum of all input fields $( inputs ).each( function () { total += +$( this ).parse( options ); }); // subtotal doesn't include the "additional" field subtotal = total - $( input_adicional ).parse( options ); // populate the SPAN's with the sums and format the nubmers $( input_subtotal ).text( subtotal ).format( options ); $( input_total ).text( total ).format( options ); }). focus( function () { // if the field contains the value 0, empty it if ( +$( this ).parse( options ) === 0 ) { this.value = ''; } }); </code></pre> <p><strong>Live demo:</strong> <a href="http://jsfiddle.net/U9V6x/" rel="nofollow">http://jsfiddle.net/U9V6x/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload