Results 1 to 5 of 5

Thread: Javascript Calculator

  1. #1
    Join Date
    May 2005
    Location
    London, UK / Brisbane, Australia
    Posts
    102
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Javascript Calculator

    Hi all,

    Wondered if a JS expert could give me a hand...
    I'm in need of a simple javascript calculator tool... all I need it to do is add up about 20 user inputed fields (HTML form) to give a total. It would something along the lines of:- car expense: [input], shopping expense: [input], etc. total spent:[displayed in input box].

    Did a search in google and script directories... but kept getting all sorts, overkill scripts. I'm sure it's real simple to do, but appreciate the help.

    Thanks

    Shiv

  2. #2
    Join Date
    Jun 2005
    Posts
    1,081
    Thanks
    4
    Thanked 15 Times in 15 Posts

    Default

    This should be easy. Simply get the form fields into variables, then just add the values together:

    Code:
    var amt1 = document.getElementById("Amount1").value;
    var amt2 = document.getElementById("Amount2").value;
    var amt3 = document.getElementById("Amount3").value;
    var amt4 = document.getElementById("Amount4").value;
     
    //-- Either hidden field for postback, or other form field for display
    var Total = document.getElementById("Total");
    Total = amt1 + amt2 + amt3 + amt4;
    You could use a loop to retrieve form values and add them to the Total but how you do that would depend on what other fields may be present.

  3. #3
    Join Date
    Feb 2004
    Posts
    4,903
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Using Suneels exmaple:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>New Document</TITLE>
    <SCRIPT type="text/javascript">
    <!--
    function calculate(){
     var field1 = parseFloat(document.getElementById("field1").value);
     var field2 = parseFloat(document.getElementById("field2").value);
     var field3 = parseFloat(document.getElementById("field3").value);
     var field4 = parseFloat(document.getElementById("field4").value);
     var field5 = parseFloat(document.getElementById("field5").value);
     var total = field1 + field2 + field3 + field4 + field5;
     
     var totaldisplay = document.getElementById("Total");
     totaldisplay.innerText = total;
    }
    //-->
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM>
    <INPUT id="field1" type="text"><BR>
    <INPUT id="field2" type="text"><BR>
    <INPUT id="field3" type="text"><BR>
    <INPUT id="field4" type="text"><BR>
    <INPUT id="field5" type="text"><BR>
    <INPUT type="button" onclick="calculate();" value="Calculate"><BR>
    Total: <SPAN id="total">0.00</SPAN>
    </FORM>
    </BODY>
    </HTML>
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  4. #4
    Join Date
    May 2005
    Location
    London, UK / Brisbane, Australia
    Posts
    102
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default

    Thanks for that guys....

    Gave it a go but doesn't seem to be doing anything. I double checked all filed names and variables, but still no joy.

  5. #5
    Join Date
    May 2005
    Location
    London, UK / Brisbane, Australia
    Posts
    102
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default

    All sorted... working perfectly.

    Thanks again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Delivery Calculator
    By s80wkr in forum ASP (VBScript)
    Replies: 2
    Last Post: 26th September 2006, 04:50 PM

Bookmarks

Posting Permissions

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