How to add values in table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vini171285
    New Member
    • Apr 2008
    • 60

    How to add values in table

    Hi..
    This is my code..
    Actually i want to add values in this table..
    Values are being added but suppose i change a value in a text box that value is added to total..
    i.e. if num1=100,num2=1 00,num3=100 then total comes 300..
    Now if i change num2=200(suppos e),then total comes 500 instead of 400...
    Can someone where the problem is??

    Code:
    <%@language="VBScript"%>
    <% option explicit %>
    <html>
    <head>
    <script type="text/Javascript">
    function check1()
    {
    document.frm.total.value=document.frm.price1.value;
    }
    function check2()
    {
    document.frm.total.value=(document.frm.total.value*1)+(document.frm.price2.value*1);
    }
    function check3()
    {
    document.frm.total.value=(document.frm.total.value*1)+(document.frm.price3.value*1);
    }
    </script>
    </head>
    <body>
    <form name=frm>
    <table border=1>
    <tr>
    <td>NUMBER 1 </td>
    <td><input type=text name=price1 onchange="check1()"></td>
    </tr>
    <tr>
    <td>NUMBER 2 </td>
    <td><input type=text name=price2 onchange="check2()"></td>
    </tr>
    <tr>
    <td>NUMBER 3 </td>
    <td><input type=text name=price3 onchange="check3()"></td>
    </tr>
    <tr>
    <td>Total</td>
    <td><input type=text name=total></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    Thanx..
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi Vini,

    Try this little snippet below - you should call the function Add from each of your onchange events and it will give you a total each time you change one. You'll need set the default value of the chekboxes to zero and put some validation in to make it work properly but this is a start.
    Code:
      
    <script type="text/Javascript">
    function Add()
    {
    var txt1 = parseInt(document.frm.price1.value)
    var txt2 = parseInt(document.frm.price2.value)
    var txt3 = parseInt(document.frm.price3.value)
    document.frm.total.value = txt1 + txt2 + txt3;
    }
    </script>
    Hope this helps, let me know how it goes.

    Dr B

    Comment

    • Vini171285
      New Member
      • Apr 2008
      • 60

      #3
      Hi DrBunchman,

      The code you gave is working..the addition of textboxes is right,but if we leave any one text box as blank i.e no value then we get no answer i.e we just get NaN as output...also until & unless we dont enter all the 3 values it gives total as NaN....

      Thanx !!

      Comment

      • DrBunchman
        Recognized Expert Contributor
        • Jan 2008
        • 979

        #4
        That's why I said you'd need to put some validation in. The parseInt() function returns 'NaN' when the first digit of the integer fails to parse (which it will obviously do when the input is blank). You'll need to check whether the parse failed or not before you try to use the value.

        Hope this helps,

        Dr B

        Comment

        • Vini171285
          New Member
          • Apr 2008
          • 60

          #5
          Hi DrBunchman
          I have done the validation and the code is working..
          Thanks

          Comment

          • DrBunchman
            Recognized Expert Contributor
            • Jan 2008
            • 979

            #6
            Well done Vini, I'm glad you got it sorted out.

            Dr B

            Comment

            Working...