Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
Calculate total sum of all the numbers previously entered in a field

i want to calculate the total of the numbers entered by the user. After a user has added item name and the amount, i want to display the total. How can i do this? i just need to display the total.

For example item name : 10 item name : 5 total = 15

http://jsfiddle.net/81t6auhd/


<body>

<header>
    <h1>Exercise 5-2</h1>
</header>


    <p>Item: <input type="text" id="item" size="30">
    <p>Amount: <input type="text" id="amount" size="30">
    <p><span id="message">*</span>
    <p><input type="button" id="addbutton" value="Add Item" onClick="processInfo();">



<script>
var $ = function(id) {
    return document.getElementById(id);
};

var myTransaction = [];
                              
function processInfo ()
{    
     var myItem = $('item').value;
     var myAmount = parseFloat($('amount').value);
     var myTotal = myItem + ":" + myAmount;
     var myParagraph = $('message');  
     

     myParagraph.innerHTML = "";
     myTransaction.push(myTotal);
     myParagraph.innerHTML += myTransaction.join("<br>"); 
        
};

(function () {
    $("addbutton").onclick = processInfo;
    
})();
</script>
</body>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you have to stored the previous value somewhere in memory to be able to reuse it at next iteration

one proposal can be to stored it in dataset of the field

     if ($('amount').dataset.previous) {
      myAmount += parseFloat($('amount').dataset.previous);
     }
     $('amount').dataset.previous = myAmount

var $ = function(id) {
    return document.getElementById(id);
};

var myTransaction = [];
                              
function processInfo ()
{    
     var myItem = $('item').value;
     var myAmount = parseFloat($('amount').value);
     if ($('amount').dataset.previous) {
      myAmount += parseFloat($('amount').dataset.previous);
     }
     $('amount').dataset.previous = myAmount;
     var myTotal = myItem + ":" + myAmount;
     var myParagraph = $('message');  
     

     myParagraph.innerHTML = "";
     myTransaction.push(myTotal);
     myParagraph.innerHTML += myTransaction.join("<br>"); 
        
};

(function () {
    $("addbutton").onclick = processInfo;
})();
<p>Item: <input type="text" id="item" size="30">
<p>Amount: <input type="text" id="amount" size="30">
<p><span id="message">*</span>
<p><input type="button" id="addbutton" value="Add Item" onClick="processInfo();">

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda