Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    adding up values

    I want to add up all the values from the right hand colum of text fields (last one in each row) and output in to the box below. Also I want to limit the amount of fields the user can add to 100.

    I thought I would be able to do this one as it looks easy. bu no :-(

    please help me one more time.

    the code


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script language="Javascript" type="text/javascript">
    var Cnt=2;

    function addField(area,field,limit) {
    if (Cnt==limit){ return; }
    if(!document.getElementById) return;
    var field_area=document.getElementById(area);
    var li=field_area.getElementsByTagName('LI')[0].cloneNode(true);
    field_area.appendChild(li);
    var ips=li.getElementsByTagName('INPUT');
    for (var zxc0=0;zxc0<ips.length;zxc0++){
    ips[zxc0].value='';
    ips[zxc0].name='friend_'+Cnt+zxc0;
    ips[zxc0].id='friend_A'+Cnt+zxc0;

    }
    Cnt++;
    }

    function addit()
    {
    document.summing.s.value = parseInt(document.summing.friend_A14.value) + parseInt(document.summing.friend_A24.value)
    }

    </script>


    </head>

    <body>
    <form name="summing">
    <br />
    <ol id="friends_area">
    <li >
    <input name="friend_11" type="text" id="friend_A11" size="4" />
    <input name="friend_12" type="text" id="friend_A12" size="20" />
    <input name="friend_13" type="text" id="friend_A13" size="10">
    <input name="friend_14" type="text" id="friend_A14" size="10">
    <input name="friend_15" type="text" id="friend_A15" size="7">
    </li>
    </ol>
    <input type="button" value="Add Another row" onClick="addField('friends_area','friend_',10);" />

    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>
    <label>
    <input name="s" type="text" id="s" size="5">
    </label>
    <input type="button" value ="calculate" name="Submit" onClick="addit()">
    <br />
    <br />
    </form>

    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 503 Times in 490 Posts
    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script language="Javascript" type="text/javascript">
    var Cnt=2;
    
    function addField(area,field,limit) {
     if (Cnt>limit){ return; }
     if(!document.getElementById) return;
     var field_area=document.getElementById(area);
     var li=field_area.getElementsByTagName('LI')[0].cloneNode(true);
     field_area.appendChild(li);
     var ips=li.getElementsByTagName('INPUT');
     for (var zxc0=0;zxc0<ips.length;zxc0++){
      ips[zxc0].value='';
      ips[zxc0].name='friend_'+Cnt+zxc0+1;
      ips[zxc0].id='friend_A'+Cnt+zxc0+1;
     }
     Cnt++;
    }
    
    function Add(obj,id){
     var totalobj=document.getElementById(id);
     if (!totalobj.ary){ totalobj.ary=[]; }
     if (!obj.set){obj.set=true; totalobj.ary.push(obj); }
     obj.value=obj.value.replace(/\D/g,''); // only digits
     var val=0;
     for (var zxc0 in totalobj.ary){
      if (totalobj.ary[zxc0].value.length>0){
       val+=totalobj.ary[zxc0].value*1;
      }
     }
     totalobj.value=val;
    }
    </script>
    
    
    </head>
    
    <body>
    Its all according how you interparate last parameter of addField( but just assign digits to meet requirement<br>
    <form name="frm" method="POST">
    <br />
    <ol id="friends_area">
    <li >
    <input name="friend_11" type="text" id="friend_A11" size="4" />
    <input name="friend_12" type="text" id="friend_A12" size="20" />
    <input name="friend_13" type="text" id="friend_A13" size="10">
    <input name="friend_14" type="text" id="friend_A14" size="10"  onkeyup="Add(this,'total4');" >
    <input name="friend_15" type="text" id="friend_A15" size="7">
    </li>
    </ol>
    <input type="button" value="Add Another row" onClick="addField('friends_area','friend_',44);" />
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input id="total4" ></td>
    </tr>
    </table>
    <br />
    <br />
    </form>
    
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks soooo much!!!!!

    I hope its not as cold in portsmouth as it is here in brighton, im freezing me knackers off!

    laters vic.

    bevan

  • #4
    New Coder
    Join Date
    Jan 2006
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    will it work in ie?

    works in safari but not my ie (might just be mac though)


  •  

    Posting Permissions

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