How to get sum of input fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harrycta
    New Member
    • May 2010
    • 4

    How to get sum of input fields

    I have created a form in asp. I used for loop to create text fields, obviously each text fields will have same name. Now I want the sum of these input fields. The sum should be displayed in total imports ( by adding 2 imports ) and total duty forgone( by adding 2 duty forgone).
    Attached Files
    Last edited by Niheel; May 19 '10, 06:54 AM. Reason: fixed grammar and punctuation, please read guidelines on how to post on bytes
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You have multiple IDs which you should correct. IDs should be unique.

    Anyway, if the name is the same for the ones that you need the sum of, use document.getEle mentsByName() to get all elements with the same name. Then loop over the collection and add them to a variable using parseInt/parseFloat to convert them to a number. Finally, set the total field to this value:
    Code:
    document.getElementById("tot_import").value = total;
    See how you manage with that and if you get stuck, post your code.

    Comment

    • harrycta
      New Member
      • May 2010
      • 4

      #3
      How to add 2 input fields

      Thanks for the replay ,

      I tried with that code.great its working. here is the code

      Code:
      function DoSum1() //for looped sum
      {
        var TheSum = 0;
        for (i=1; i<=5; i++) {
          TheSum += parseFloat(document.getElementById("txtExp_Fy1" + i).value-0);
        }
        document.getElementById("txtExp_tot1").value = TheSum;
      }

      i was able to get the result for vertical columns but now the problem is to get the sum of horizontal i.e row wise total
      Last edited by Dormilich; May 19 '10, 01:39 PM. Reason: Please use [code] tags when posting code

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You'd get it in a similar manner, but it depends on how you're naming/ID-ing your text box elements vertically.

        Comment

        • harrycta
          New Member
          • May 2010
          • 4

          #5
          How to get the sum of txt fields

          Thanks for the replay again,

          Dear sir,
          Here is the code written in "ASP" , Please tell me "How i can find the sum of text fields horizontally.


          Code:
          <html>
          <head>
          <TITLE>EXIM - Progress Reports - Annual Details (Insert/Update) - Software Technology Parks Of India</title>
          </HEAD>
          <Body bgcolor="#FFFFFF" onload="return grand_tot();">
          <Form Action="Annual9.asp" method=post Name=frm >
          <Table border=1 cellspacing="0" cellpadding="0" align="center" width="60%" bgcolor=#F6E3CE>
          <Tr Bgcolor=#DF7401>
          	<td height="22" Align="left" colspan=13>
          		<Font color="#111111" Face="Arial" Size="2">
          		<B>16(a).&nbsp;CG Import from the date of Inception up to 21.03.2010[all Amount Rs in Lakhs]</B>
          		</Font>
          	</td>
          </Tr>
          <tr Bgcolor=#DF7401> 
          		  <td width="3%"   align="center" valign="middle" rowspan="2"><font size="2" face="Arial, Helvetica" class="content"><b>Sl.No</b></font></td>
          		  <td width="20%"  align="center" rowspan="2"><b>Imports Through </b></td>	
                    <td width="20%"  align="center" colspan="2"><b>As on 31.03.2009(From the Inception)</b></td>
          		  <td width="20%"  align="center" colspan="2"><b>During Financial Year 2009-10</b></td>
          		  <td width="20%"  align="center" colspan="2"><b>Total</b></td>
                		  
          </tr>	
          <tr Bgcolor=#DF7401> 
          		  
          		  <td width="10%"  align="center"><b>Import</b></td>	
                    <td width="10%"  align="center"><b>Duty Foregone</b></td>
          		  <td width="10%"  align="center"><b>Import</b></td>	
                    <td width="10%"  align="center"><b>Duty Foregone</b></td>
                    <td width="10%"  align="center"><b>Import</b></td>	
                    <td width="10%"  align="center"><b>Duty Foregone</b></td> 	  
          </tr>
          <%
            
          Dim i
          
          for i=1 to 10
          
          if i=1 Then 
          %>
          <tr > 
                    <td width="3%"  align="center">a</td>
          		  <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Out right Purchase</b></font></td>
          <%
          elseIf i=2 then
          %>
          <tr > 
                    <td width="3%"  align="center">b</td>
          		  <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Loan Basis</b></font></td>
          <%
          elseIf i=3 then
          %>
          <tr > 
                    <td width="3%"  align="center">c</td>
          		  <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Free of Charge</b></font></td>
          <%
          elseIf i=4 then
          %>
          <tr > 
                    <td width="3%"  align="center">d</td>
          		  <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>High Sea Sale</b></font></td>
          <%
          elseIf i=5 Then 
          %>
          <tr > 
                    <td width="3%"  align="center">e</td>
          		  <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Second Hand CG</b></font></td>
          <%
          elseIf i=6 then
          %>
          <tr > 
                    <td width="3%"  align="center">f</td>
          		  <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Bond-Bond Transfer</b></font></td>
          
          <%
          elseIf i=7 then
          %>
          <tr > 
                    <td width="3%"  align="center">g</td>
          		  <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Deemed Import</b></font></td>
          
          <%
          elseIf i=8 then
          %>
          <tr > 
                    <td width="3%"  align="center">h</td>
          		  <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Import through courier mode</b></font></td>
          <%
          elseIf i=9 Then 
          %>
          <tr > 
                    <td width="3%"  align="center">i</td>
          		  <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Leasing of Capital Goods</b></font></td>
          <%
          elseIf i=10 Then 
          %>
          <tr > 
                    <td width="3%"  align="center">j</td>
          		  <td width="20%"   align="center" valign="middle"><font size="1" face="Arial, Helvetica" class="content"><b>Permanent shifting from other units</b></font></td>
          <%End If
          %>
          <td width=10%   align=center> <input type=text name=txtExp_Fy11<%=i%> id="txtExp_Fy1"  size=8  value="<%=Request("txtExp_Fy11"&i&"")%>"                       
           Onfocus="window.status='Enter Manpower from the day of inception' ;return true" onkeypress="return validateint(event);"
           Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
           </td> 
           <td width=10%   align=center> <input type=text name=txtExp_inc12<%=i%>  id="txtExp_inc1" size=8   value="<%=Request("txtExp_inc12"&i&"")%>"
            Onfocus="window.status='Enter Manpower from the day of inception' ;return true" onkeypress="return validateint(event);"
           Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
           </td>  
           <td width=10%   align=center> <input type=text name=txtExp_Fy13<%=i%> id="txtExp_Fy1"  size=8  value="<%=Request("txtExp_Fy13"&i&"")%>"                       
           Onfocus="window.status='Enter Manpower from the day of inception' ;return true" onkeypress="return validateint(event);"
           Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
           </td> 
           <td width=10%   align=center> <input type=text name=txtExp_inc14<%=i%>  id="txtExp_inc1" size=8   value="<%=Request("txtExp_inc14"&i&"")%>"
            Onfocus="window.status='Enter Manpower from the day of inception' ;return true" onkeypress="return validateint(event);"
           Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
           </td>  
           <td width=10%   align=center> <input type=text name=txtExp_Fy15<%=i%> id="txtExp_Fy1"  size=8  value="<%=Request("txtExp_Fy15"&i&"")%>"                       
           Onfocus="window.status='Enter Manpower from the day of inception' ;return true" readonly="readonly" onkeypress="return validateint(event);" onblur="return DoSum33() ;"
           Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
           </td> 
           <td width=10%   align=center> <input type=text name=txtExp_inc16<%=i%>  id="txtExp_inc1" size=8   value="<%=Request("txtExp_inc16"&i&"")%>"
            Onfocus="window.status='Enter Manpower from the day of inception' ;return true"  readonly="readonly"  onkeypress="return validateint(event);" onblur="return DoSum34() ;"
           Onmouseover="window.status='Manpower from the day of inception'';return true;" Onmouseout="window.status='Insertion form';return true;">
           </td>  
           </tr>
          <%
          next
          %>
          <tr align="center"> 
                    <td width="3%"  align="center"><b>A</b></td>
          		  <td width="20%"   align="center" valign="middle"><font size="02" face="Arial, Helvetica" class="content"><b>Grand Total(a to j)</b></font></td>
          		  <td width="10%" ><input type="text" name="txt_grandtot_inc_import" size="8" onkeypress="return validateint(event);" readonly="readonly" ></td>	
                    <td width="10%" ><input type="text" name="txt_grandtot_inc_dutyforegone" size="8" onkeypress="return validateint(event);" readonly="readonly" ></td>
          		  <td width="10%" ><input type="text" name="txt_grandtot_during_import" size="8" onkeypress="return validateint(event);" readonly="readonly" ></td>
                    <td width="10%" ><input type="text" name="txt_grandtot_during_dutyforegone" size="8" onkeypress="return validateint(event);" readonly="readonly" ></td>				
                    <td width="10%" ><input type="text" name="txt_grandtot_tot_import" size="8" onkeypress="return validateint(event);" readonly="readonly"  "></td>
                    <td width="10%" ><input type="text" name="txt_grandtot_tot_foregone" size="8" onkeypress="return validateint(event);" readonly="readonly" "></td>				  
          </tr> 
          </tr> 
          </Table>
          </Body>
          </html>

          please check the attachement , you will get the clear picture what i real want to calculate. waiting for your replay ..........
          Attached Files
          Last edited by acoder; May 20 '10, 10:20 AM. Reason: Please use [code] tags

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            See how you've used the ID here:
            Code:
            document.getElementById("txtExp_Fy1" + i)
            Give the IDs of the elements going across similar style IDs, e.g. "textExp_Fy1_1" , "textExp_Fy 1_2" and so on, so then you can use a for loop to loop over them as you've done with the previous code.

            Comment

            • harrycta
              New Member
              • May 2010
              • 4

              #7
              How to get the sum of text fields.

              Thaks a lot. I tried with the fallowing code .Its working.

              Code:
              function loopsum6() //to get the sum in horizontal way i.e alternate values
              {
              	var count=1;
              	while(count<=2)
              	{	
              		for (i=1; i<=2; i++)
              		{
              			if(count==1 && i==1)
              			{
              				var val1 = parseFloat(document.getElementById("txtExp_inc1289"+ i).value-0);
              				var val2 = parseFloat(document.getElementById("txtExp_inc1489"+ i).value-0);
              				var ansD = document.getElementById("txtExp_inc1689"+ i);
              				ansD.value = val1 + val2;
              				break;
              			}
              			if(count==2 && i==2)
              			{
              				var val1 = parseFloat(document.getElementById("txtExp_inc1289"+ i).value-0);
              				var val2 = parseFloat(document.getElementById("txtExp_inc1489"+ i).value-0);
              				var ansD = document.getElementById("txtExp_inc1689"+ i);
              				ansD.value = val1 + val2;
              				break;
              			}			
              		}
              		count++;
              	}
              }
              Last edited by gits; May 21 '10, 09:50 AM. Reason: added code tags

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                That's not the most efficient piece of code - a lot of unnecessary bloat and redundancy.

                PS. threads merged. Please keep all posts in one thread instead of starting a new thread each time!

                Comment

                Working...