Javascript code problem

Discussion in 'Computer Support' started by Mike Walker, May 31, 2005.

  1. Mike Walker

    Mike Walker Guest

    HHHHHEEEEEELLLLLPPPPPP

    I have a HTML page, with text boxes in
    Salary ; Benefits ; Other
    I want to total them up on the page, so I wrote the following:-

    <script language="javascript"><!--//
    function autocalc()
    { var fin=0;
    if (StepOneData.App1_Salary.value == 0 ){ StepOneData.App1_Salary.value =
    0 }
    if (StepOneData.App1_Benefits.value == 0 ){ StepOneData.App1_Benefits.value
    = 0 }
    if (StepOneData.App1_Other.value == 0 ){ StepOneData.App1_Other.value = 0 }
    a=StepOneData.App1_Salary.value;
    b=StepOneData.App1_Benefits.value;
    c=StepOneData.App1_Other.value;
    fin= (a+b+c);
    StepOneData.TotalApp1.value = (a+b+c);
    }
    //--></SCRIPT>

    but if I type in 10.00 ; 20.00 ; 30.00 it should show 60.00 but it shows
    10:0020:0030:00

    I other words it treating the text as tect and not numbers, so I added this
    at the top:
    var a=1;
    var b=1;
    var c=1;

    To force it to numbers but its the same :-(

    I know the code works because if i subtract the numbers it works fine, but
    adding is not.

    Please, tell me where im going wrong, I need to tell the code to use numbers
    not text.

    Thanks
    Mike.





    --


    ----------------------------------------------------------------------------
    ----------------------------------------

    Why not visit one of my web sites:-
    http://www.feedback.gb.com - Grimsby's Best Rock Band
    http://www.charltontravel.co.uk - A travel site I help to maintain.
    All e-mails are scanned with Sophos Anti-Virus - Visit http://www.sohos.com

    6*9=42
    R.I.P. Douglas Adams - http://www.douglasadams.com/
     
    Mike Walker, May 31, 2005
    #1
    1. Advertisements

  2. Mike Walker

    Mike Walker Guest

    Sorry I should have also posted the way I send the data:-

    <tr>
    <td>Applicate 1</td>
    <td><input type="text" name="App1_DOB" size="10" maxlength="10"></td>
    <td><input type="text" name="App1_Salary" size="10" maxlength="10"></td>
    <td><input type="text" name="App1_Benefits" size="10"
    maxlength="10"></td>
    <td><input type="text" name="App1_Other" size="10" maxlength="10"
    onBlur="autocalc();"></td>
    <td><input type="text" name="TotalApp1" value="0.00" readonly
    style="border: none;"></td>
    </tr>

    Thanks
    Mike.


    --


    ----------------------------------------------------------------------------
    ----------------------------------------

    Why not visit one of my web sites:-
    http://www.feedback.gb.com - Grimsby's Best Rock Band
    http://www.charltontravel.co.uk - A travel site I help to maintain.
    All e-mails are scanned with Sophos Anti-Virus - Visit http://www.sohos.com

    6*9=42
    R.I.P. Douglas Adams - http://www.douglasadams.com/
     
    Mike Walker, May 31, 2005
    #2
    1. Advertisements

  3. Mike Walker

    Mike Walker Guest

    Ive solved it:-

    function autocalc()
    { var fin=0;

    if (StepOneData.App1_Salary.value == 0 ){ StepOneData.App1_Salary.value =
    0 }
    if (StepOneData.App1_Benefits.value == 0 ){ StepOneData.App1_Benefits.value
    = 0 }
    if (StepOneData.App1_Other.value == 0 ){ StepOneData.App1_Other.value = 0 }

    a=(StepOneData.App1_Salary.value);
    b=(StepOneData.App1_Benefits.value);
    c=(StepOneData.App1_Other.value);

    StepOneData.TotalApp1.value = ((a++)+(b++)+(c++)); //this works!
    }
    //--></SCRIPT>

    By adding the a++ ; b++ ; c++

    But Why?




    --


    ----------------------------------------------------------------------------
    ----------------------------------------

    Why not visit one of my web sites:-
    http://www.feedback.gb.com - Grimsby's Best Rock Band
    http://www.charltontravel.co.uk - A travel site I help to maintain.
    All e-mails are scanned with Sophos Anti-Virus - Visit http://www.sohos.com

    6*9=42
    R.I.P. Douglas Adams - http://www.douglasadams.com/
     
    Mike Walker, Jun 1, 2005
    #3
  4. Mike Walker

    why? Guest

    Try
    http://www.w3schools.com/js/default.asp
    and if you are working with numbers input type="number" and not input
    type="text" look for the calculator examples to see how to input
    numbers.
    http://javascript.internet.com/calculators/

    No other variables declared?
    If test=0 then set same value to 0?????
    The 5 seconds glance shows -

    Addition and String cat are both + , ++ most likely forces a number type
    conversion which isn't the correct method. This is because if you have
    x=5 , x++ makes x=6.


    See http://www.w3schools.com/js/js_operators.asp

    Me
     
    why?, Jun 1, 2005
    #4
  5. Mike Walker

    Joe Guest

    parseInt("String",base) converts a string to an integer
    parseFloat("String") converts a string to a floating point number
    I learned the difference between floating point numbers and integers a
    long time ago, I also learned it the hard way.
     
    Joe, Jun 1, 2005
    #5
  6. Mike Walker

    Mike Walker Guest

    Thanks for the help.

    I had further problems but found that if I use
    (1*a)+(1*b)+(1*c) also worked

    Mike.


    --


    ----------------------------------------------------------------------------
    ----------------------------------------

    Why not visit one of my web sites:-
    http://www.feedback.gb.com - Grimsby's Best Rock Band
    http://www.charltontravel.co.uk - A travel site I help to maintain.
    All e-mails are scanned with Sophos Anti-Virus - Visit http://www.sohos.com

    6*9=42
    R.I.P. Douglas Adams - http://www.douglasadams.com/
     
    Mike Walker, Jun 1, 2005
    #6
  7. Mike Walker

    why? Guest

    That's because you are most likely forcing type conversion to numbers.
    Try www.google.com and search for javascript tutorials.
    <snip>

    Me
     
    why?, Jun 2, 2005
    #7
  8. Mike Walker

    aditya12

    Joined:
    Feb 4, 2019
    Messages:
    1
    Likes Received:
    0
    Code:
    <script>
       var myObj = new Object();      // or you can write: var myObj = new Object;
        myObj.name = "Opal Kole";
       myObj.address = "63 street Ct.";
       myObj.age = 48;
       myObj.married  = true;
    
       document.writeln("name" in myObj);      // Returns true
        document.writeln("birthdate" in myObj); // Returns false
                                            // birthdate propery not in myObj
        document.writeln("address" in myObj);    // Returns true
        document.writeln("age" in myObj);        // Returns true
        document.writeln("married" in myObj);    // Returns true
    </script>
    
    work for me
     
    Last edited by a moderator: Feb 4, 2019
    aditya12, Feb 4, 2019
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.