www.webdeveloper.com
Results 1 to 6 of 6

Thread: adding numbers together

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    36

    adding numbers together

    Hi Does anyone know what i'm doing wrong here. Instead of adding my numbers together its putting them next to each other?

    This is the javascript...

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    
    <!--
    var myIncome1;
    var myIncome2;
    var myIncome3;
    var myIncome4;
    
    
    function HowMany(form)
    {
    var totalincome;
    totalincome = (myIncome1 + myIncome2 + myIncome3 + myIncome4);
    form.Tincome.value = totalincome;
    }
    
    function SetMyIncome1(inc1)
    {
    myIncome1 = inc1.value;
    }
    
    function SetMyIncome2(inc2)
    {
    myIncome2 = inc2.value;
    }
    
    function SetMyIncome3(inc3)
    {
    myIncome3 = inc3.value;
    }
    
    function SetMyIncome4(inc4)
    {
    myIncome4 = inc4.value;
    }
    
    function ClearForm(form){
    
    form.myIncome1.value = "";
    form.myIncome2.value = "";
    form.myIncome3.value = "";
    form.myIncome4.value = "";
    form.Tincome.value = "";
    
    
    }
    
    
    // -->
    
    </SCRIPT>
    This is the form...

    Code:
    <div><form method="Post">
    <div><p>Monthly income</p></div>
    <div><p>Pay <input name="myIncome1" type="text" ONCHANGE="SetMyIncome1(this)"/></p>
    <p>Benefits <input name="myIncome2" type="text" ONCHANGE="SetMyIncome2(this)"/></p>
    <p>Pension <input name="myIncome3" type="text" ONCHANGE="SetMyIncome3(this)"/></p>
    <p>Other income <input name="myIncome4" type="text" ONCHANGE="SetMyIncome4(this)"/></p>
    <p><INPUT TYPE=BUTTON ONCLICK="HowMany(this.form)" VALUE="Calculate Income"><INPUT TYPE=BUTTON VALUE=" Reset " onClick="ClearForm(this.form)"></p>
    
    
    <p>Total <input name="Tincome" type="text" value="" /></p></div></form

  2. #2
    Join Date
    Dec 2010
    Posts
    36
    Fixed it....

    Code:
    totalincome = parseFloat(form.myIncome1.value)+parseFloat(form.myIncome2.value)+parseFloat(form.myIncome3.value)+parseFloat(form.myIncome4.value);
    Does anyone know how to set the value to 0 if they don't enter a value? I get NaN if they miss a box but dont want to put a check form on as they dont have to fill out all the boxes.

  3. #3
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    if (isNaN(blah))
    blah=0;

  4. #4
    Join Date
    Dec 2010
    Posts
    36
    thank you :-)

  5. #5
    Join Date
    Apr 2010
    Location
    Cocoa Beach, FL
    Posts
    41

    javascript debugging

    javascript can be very had to debug sometimes due to the lack of detailed error messages.

    + is the default string concatenation function, unless you specifically cast your vars as number types, + will treat them as if they are strings.

    Michael G. Workman
    michael.g.workman@gmail.com

  6. #6
    Join Date
    Dec 2010
    Posts
    36
    thanks, it took me a while but I figured it out. I'm getting used to it a bit now but its very complicated!! :-)

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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



Recent Articles