Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Get Array Sum

  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    34
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Get Array Sum

    Hi, today I am creating a form where a user can enter prices for things. I want to use javascript to display a total so far and it should update automatically as the user enters/changes new prices.
    I have a form which looks something like this:
    Code:
    <form method="post" action="mailer.php" name="contact">
    Price 1 <input type="text" name="stampprice[]" size="5" onchange="calculate_total()">
    Price 2 <input type="text" name="stampprice[]" size="5" onchange="calculate_total()">
    Price 3 <input type="text" name="stampprice[]" size="5" onchange="calculate_total()">
    Price 4 <input type="text" name="stampprice[]" size="5" onchange="calculate_total()">
    Price 5 <input type="text" name="stampprice[]" size="5" onchange="calculate_total()">
    Price 6 <input type="text" name="stampprice[]" size="5" onchange="calculate_total()">
    Price 7 <input type="text" name="stampprice[]" size="5" onchange="calculate_total()">
    <br>
    Total <input type="text" name="grand" readonly="yes" id="grand">
    </form>
    Here is my javascript function, calculate_total():
    Code:
    function calculate_total() {
    	var all = document.getElementsByName("stampprice[]");
    	var total = 0;
    	for (i=0; i <= all.length; i++) {
    		var price = document.getElementsByName("stampprice[]")[i].value;
    		var total = total + price;
    	}
    	document.getElementById("grand").value=total;
    }
    When I change the values in the price fields, nothing happens. I also tried putting javascript:calculate_total(); in the address bar, but still, it doesn't work. Help?
    Last edited by Cloud Ghost; 12-11-2011 at 08:35 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Here you are:-

    Code:
    <script type = "text/javascript">
    
    function calculate_total() {
    var total = 0;
    var all = document.getElementsByName("stampprice[]");
    for (i=0; i <all.length; i++) {
    var price = (all[i].value*1) || 0;  // price must be a number
    total = total + price;
    }
    document.getElementById("grand").value=total;
    }
    
    </script>

    Quizmaster: What word is used for the letter "B" in the phonetic alphabet?
    Contestant: Bob

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Cloud Ghost (12-11-2011)


  •  

    LinkBacks (?)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •