Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda