Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!