Loop and sum

Discussion in 'Javascript' started by staeri@gmail.com, Apr 18, 2006.

  1. Guest

    I have an unknown number of textboxes which I want to sum. They are
    named something like this:

    "ctl00$ContentPlaceHolder1$DataList1$ctl03$DataList2$ctl03$txtSumB"

    The only thing i know for sure is that "txtSumB" is somewhere in the
    name. The page can contain anything from 1 to 50 textboxes.

    Can someone please help me with a script which loops through all the
    textboxes with "txtSumB" in the name and sums the values?

    Very grateful for help!

    Regards,

    S
     
    , Apr 18, 2006
    #1
    1. Advertising

  2. RobG Guest

    wrote:
    > I have an unknown number of textboxes which I want to sum. They are
    > named something like this:
    >
    > "ctl00$ContentPlaceHolder1$DataList1$ctl03$DataList2$ctl03$txtSumB"
    >
    > The only thing i know for sure is that "txtSumB" is somewhere in the
    > name. The page can contain anything from 1 to 50 textboxes.
    >
    > Can someone please help me with a script which loops through all the
    > textboxes with "txtSumB" in the name and sums the values?


    Get the elements within the form, find those with a name that has the
    string in it and add them up.

    <script type="text/javascript">

    function showTotal(form, totalEl)
    {
    var el, els = form.elements;
    var sum = 0;

    for (var i=0, num=els.length; i<num; ++i){
    el = els;

    if ('text' == el.type && /SumB/.test(el.name)){
    sum += +el.value;
    }
    form.elements[totalEl].value = sum;
    }
    }
    </script>

    <form action=""><div>
    <input type="text" name="ZugZug">ZugZug<br>
    <input type="text" name="fooSumBxx">fooSumBxx<br>
    <input type="text" name="barSumByy">barSumByy<br>
    <input type="text" name="sueSumBzz">sueSumBzz<br>
    <input type="text" name="formTotal" readonly>Total
    <input type="button" value="Show total"
    onclick="showTotal(this.form, 'formTotal');">
    </div></form>


    Note that you need to test the value of each input to see if it's a
    number and suitable to be added before actually adding it.


    --
    Rob
     
    RobG, Apr 18, 2006
    #2
    1. Advertising

  3. Guest

    Thank's a lot!
     
    , Apr 18, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryce (Work)

    Grouping and Sum problem

    Bryce (Work), Sep 30, 2003, in forum: XML
    Replies:
    3
    Views:
    507
    Bryce (Work)
    Oct 1, 2003
  2. Chris
    Replies:
    5
    Views:
    1,867
    Chris
    Apr 19, 2005
  3. Paddy

    sum and strings

    Paddy, Aug 18, 2006, in forum: Python
    Replies:
    56
    Views:
    1,320
    Fredrik Lundh
    Aug 25, 2006
  4. Erik Max Francis
    Replies:
    15
    Views:
    468
    Dan Stromberg
    Aug 19, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    391
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page