adding column totals

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abby Lee

    adding column totals

    Can someone tell me what is wrong with this statement?

    function subtotal(){
    var ttot = 1;
    for (var i = 1; i < 7; i++){
    if(isPosInt(doc ument.forms[0].total(i).value )){
    ttot += document.forms[0].total(i).value ;
    }
    document.forms[0].subtot.value = format(ttot);
    }
    }

    I know the "isPosInt" function works because it works for other things and
    Mike wrote it...so I did not add it.
    And I do have a global varible elsewhere var e = document.forms[0].elements,
    but I also can't get "if(isPosIn t(ea['total' + i].value))" to work
  • McKirahan

    #2
    Re: adding column totals

    "Abby Lee" <abbylee26@hotm ail.com> wrote in message
    news:uiucguest. 20040922171213$ 69bb@news.ks.ui uc.edu...[color=blue]
    > Can someone tell me what is wrong with this statement?
    >
    > function subtotal(){
    > var ttot = 1;
    > for (var i = 1; i < 7; i++){
    > if(isPosInt(doc ument.forms[0].total(i).value )){
    > ttot += document.forms[0].total(i).value ;
    > }
    > document.forms[0].subtot.value = format(ttot);
    > }
    > }
    >
    > I know the "isPosInt" function works because it works for other things and
    > Mike wrote it...so I did not add it.
    > And I do have a global varible elsewhere var e =[/color]
    document.forms[0].elements,[color=blue]
    > but I also can't get "if(isPosIn t(ea['total' + i].value))" to work[/color]


    Will this help? Watch for word-wrap.

    <html>
    <head>
    <title>subtotal .htm</title>
    <script type="text/javascript">
    function subtotal() {
    var ttot = 0;
    for (var i=0; i<7; i++) {
    var temp = document.forms[0].total(i).value ;
    if (isPosInt(temp) ) {
    ttot += parseInt(temp,1 0);
    }
    }
    document.forms[0].subtot.value = format(ttot);
    }
    function isPosInt() {
    // your code here!
    return true;
    }
    function format(what) {
    // your code here!
    return what;
    }
    </script>
    <style type="text/css">
    ..numb { text-align:right }
    </style>
    </head>
    <body>
    <form>
    <br><input type="text" name="total" size="5" value="1" class="numb">
    <br><input type="text" name="total" size="5" value="2" class="numb">
    <br><input type="text" name="total" size="5" value="3" class="numb">
    <br><input type="text" name="total" size="5" value="4" class="numb">
    <br><input type="text" name="total" size="5" value="5" class="numb">
    <br><input type="text" name="total" size="5" value="6" class="numb">
    <br><input type="text" name="total" size="5" value="7" class="numb">
    <br>=======
    <br><input type="text" name="subtot" size="5" value="" class="numb">
    <br><input type="button" value="Total!" onclick="subtot al()">
    </form>
    </body>
    </html>

    Obviously you should remove the values for all "total" fields.


    Comment

    • Michael Winter

      #3
      Re: adding column totals

      On Wed, 22 Sep 2004 12:18:36 -0500, Abby Lee <abbylee26@hotm ail.com> wrote:
      [color=blue]
      > Can someone tell me what is wrong with this statement?
      >
      > function subtotal(){
      > var ttot = 1;
      > for (var i = 1; i < 7; i++){
      > if(isPosInt(doc ument.forms[0].total(i).value )){[/color]

      Don't use parentheses for subscripting. Not only is it wrong, but only IE
      will accept it.
      [color=blue]
      > ttot += document.forms[0].total(i).value ;[/color]

      This will act as string concatenation. Use:

      ttot += (+document.form s[0].total[i].value);

      The unary plus will force the value to a number.

      I also suspect that 'total[i]' is incorrect. If the form control is named,
      total<N>, where <N> is number, see later in this post. My suggestion will
      apply here, too.

      [snip]
      [color=blue]
      > And I do have a global varible elsewhere var e =
      > document.forms[0].elements,[/color]

      It should probably be local.
      [color=blue]
      > but I also can't get "if(isPosIn t(ea['total' + i].value))" to work[/color]

      How is 'ea' set? In the previous thread, you used

      var ea = document.forms[0].elements['elementName'];

      What you need is:

      var ea = document.forms[0].elements;

      which you already have as the value of the variable, e.

      If 'i' in the expression

      ea['total' + i]

      was 1, it would look up the control, 'total1' in the first form.

      I probably didn't point you to the FAQ notes on accessing form controls. I
      should have. The link below covers the basics. The resource linked at the
      end of section goes into more detail.

      <URL:http://jibbering.com/faq/#FAQ4_13>

      Hope that helps,
      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Abby Lee

        #4
        Re: adding column totals

        > function subtotal() {[color=blue]
        > var ttot = 0;
        > for (var i=0; i<7; i++) {
        > var temp = document.forms[0].total(i).value ;
        > if (isPosInt(temp) ) {
        > ttot += parseInt(temp,1 0);
        > }
        > }
        > document.forms[0].subtot.value = format(ttot);
        > }[/color]

        The code did not work fo me.
        If I put an alert() message before the "var temp=document.f orms[0].total
        (i).value", I will see it when it trigger the code. But if I put an alert
        after I will not see it.

        Comment

        • McKirahan

          #5
          Re: adding column totals

          "Abby Lee" <abbylee26@hotm ail.com> wrote in message
          news:uiucguest. 20040922182716$ 4f29@news.ks.ui uc.edu...[color=blue][color=green]
          > > function subtotal() {
          > > var ttot = 0;
          > > for (var i=0; i<7; i++) {
          > > var temp = document.forms[0].total(i).value ;
          > > if (isPosInt(temp) ) {
          > > ttot += parseInt(temp,1 0);
          > > }
          > > }
          > > document.forms[0].subtot.value = format(ttot);
          > > }[/color]
          >
          > The code did not work fo me.
          > If I put an alert() message before the "var temp=document.f orms[0].total
          > (i).value", I will see it when it trigger the code. But if I put an alert
          > after I will not see it.[/color]

          What's your browser?

          I only tested it under IE5.5.



          Comment

          Working...