function only works on last row

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

    function only works on last row

    You all have been much help with my javascript needs...but I need you
    again.

    Mike gave me this...to add up any of the 7 columns that are changed.
    function autoReimb(val, itm) {
    var e = document.forms[0].elements,
    r = 'autoReimb' + itm,
    t = 0;
    if(isPosInt(val .value)) {
    e[r].value = format(val.valu e * 0.375);
    for(var i = 1; i <= 7; ++i) {
    if(isPosInt(e['miles' + i].value)) {
    t += +e['autoReimb' + i].value;
    }
    }
    e['autotot'].value = format(t);
    lineTotal(val,i tm); <-- I added
    } else {
    e[r].value = '';
    }
    } //End Function

    I added the function call to lineTotal(val,i tm) in order to total the
    row...which is also changed when the column was changed.

    function lineTotal(val, itm){
    var e = document.forms[0].elements,
    trvtps = new Array('autoReim b','airRail','c ar','taxi','lod ge','meals','mi sc');
    if(isPosInt(val .value)){
    t = 0;
    for (var i = 0; i < trvtps.length; i++){
    var trvitem = trvtps[i];
    if(isPosInt(e[trvitem + itm].value)){
    t += +e[trvitem + itm].value;
    }
    }
    e['total' + itm].value = format(t);
    }
    }

    Problem is this only works on the last row...and I don't understand
    why. I turned my last row into a remark line and then the row above it
    which was not the last line worked great but the others did not add up
    correctly.
  • Stephen Chalmers

    #2
    Re: function only works on last row


    "Abby Lee" <abbylee26@hotm ail.com> wrote in message
    news:80504bef.0 409081247.6bb8a 1f@posting.goog le.com...

    Both functions are sharing the global variable 't'.
    Declare variables with the 'var' keyword.

    --
    Stephen Chalmers



    Comment

    • Michael Winter

      #3
      Re: function only works on last row

      On Wed, 8 Sep 2004 22:16:04 +0100, Stephen Chalmers
      <NoThIsPaRtTcan befound@mail.co m> wrote:
      [color=blue]
      > "Abby Lee" <abbylee26@hotm ail.com> wrote in message
      > news:80504bef.0 409081247.6bb8a 1f@posting.goog le.com...
      >
      > Both functions are sharing the global variable 't'.
      > Declare variables with the 'var' keyword.[/color]

      Only the lineTotal function declares a global variable, t. The declaration
      of t in autoReimb is part of the var statement (notice commas, not
      semi-colons).

      Abby, could you show a sample of the HTML (a URL, preferably)? It might
      shed some light.

      Mike

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

      Comment

      • Abby Lee

        #4
        Re: function only works on last row

        > Abby, could you show a sample of the HTML (a URL, preferably)? It might[color=blue]
        > shed some light.
        >
        > Mike[/color]

        Took some doing (students only have access to the internal servers).

        You can see that if someone enters milage that part works fine...and I
        plan to make a simular function for each column. However the rows also
        must add up...

        The page

        My javascript page

        Comment

        • Abby Lee

          #5
          Re: function only works on last row

          abbylee26@hotma il.com (Abby Lee) writes:
          [color=blue][color=green]
          > > Abby, could you show a sample of the HTML (a URL, preferably)? It might
          > > shed some light.
          > >
          > > Mike[/color]
          >
          > Took some doing (students only have access to the internal servers).
          >
          > You can see that if someone enters milage that part works fine...and I
          > plan to make a simular function for each column. However the rows also
          > must add up...
          >
          > The page
          > http://www.ncsa.uiuc.edu/Divisions/Admin/reimb.asp
          > My javascript page
          > http://www.ncsa.uiuc.edu/Divisions/Admin/reimb.js[/color]

          Forget the reimb.js file...I just put the javascript code into the .asp file
          to make for easy viewing.
          thanks.

          Comment

          • Michael Winter

            #6
            Re: function only works on last row

            On 9 Sep 2004 06:35:21 -0700, Abby Lee <abbylee26@hotm ail.com> wrote:

            [snip]
            [color=blue]
            > Took some doing (students only have access to the internal servers).[/color]

            And I believe it paid off. After a quick glance, it seems that the problem
            is the relationship between the id and name attributes in the HTML. Whilst
            the attributes don't have to match[1], you shouldn't use an id that
            matches a name on another control. The reason is that when you look up an
            element, ids are checked first, then names if a match wasn't found. So,
            when you use

            document.forms['formName'].elements['elementName']

            You're expecting to find a control named elementName. What's actually
            happening though is you're finding a control with the id, elementName.

            By the way, consider using regular expressions to validate inputs. Your
            validation routines are far more complicated than they need to be.

            Mike


            [1] In the case of things like multiple, grouped checkboxes, they must not
            otherwise you'd have non-unique ids.

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

            Comment

            • Abby Lee

              #7
              Re: function only works on last row

              "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message news:<opseb3ji0 6x13kvk@atlanti s>...[color=blue]
              > On 9 Sep 2004 06:35:21 -0700, Abby Lee <abbylee26@hotm ail.com> wrote:
              >
              > [snip]
              >[color=green]
              > > Took some doing (students only have access to the internal servers).[/color]
              >
              > And I believe it paid off. After a quick glance, it seems that the problem
              > is the relationship between the id and name attributes in the HTML. Whilst
              > the attributes don't have to match[1], you shouldn't use an id that
              > matches a name on another control. The reason is that when you look up an
              > element, ids are checked first, then names if a match wasn't found. So,
              > when you use
              >
              > document.forms['formName'].elements['elementName']
              >
              > You're expecting to find a control named elementName. What's actually
              > happening though is you're finding a control with the id, elementName.
              >[/color]
              I think I understand what you are saying but am unable to see how to
              make it work in my function.

              function lineTotal(val, itm){
              var ea=document.for ms[0].elements['elementName'],
              trvtps = new Array('autoReim b','airRail','c ar','taxi','lod ge','meals','mi sc');
              if(isPosInt(val .value)){
              ta = 0;
              for (var i = 0; i < trvtps.length; i++){
              var trvitem = trvtps[i];
              if(isPosInt(ea[trvitem + itm].value)){
              ta += +ea[trvitem + itm].value;
              }
              }
              ea['total' + itm].value = format(ta);
              }
              }

              Comment

              • Michael Winter

                #8
                Re: function only works on last row

                On 15 Sep 2004 07:23:39 -0700, Abby Lee <abbylee26@hotm ail.com> wrote:
                [color=blue]
                > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                > news:<opseb3ji0 6x13kvk@atlanti s>...[color=green]
                >> On 9 Sep 2004 06:35:21 -0700, Abby Lee <abbylee26@hotm ail.com> wrote:
                >>
                >> [snip]
                >>[color=darkred]
                >> > Took some doing (students only have access to the internal servers).[/color]
                >>
                >> And I believe it paid off. After a quick glance, it seems that the
                >> problem
                >> is the relationship between the id and name attributes in the HTML.
                >> Whilst
                >> the attributes don't have to match[1], you shouldn't use an id that
                >> matches a name on another control. The reason is that when you look up
                >> an
                >> element, ids are checked first, then names if a match wasn't found. So,
                >> when you use
                >>
                >> document.forms['formName'].elements['elementName']
                >>
                >> You're expecting to find a control named elementName. What's actually
                >> happening though is you're finding a control with the id, elementName.
                >>[/color]
                > I think I understand what you are saying but am unable to see how to
                > make it work in my function.[/color]

                I wasn't suggesting a change to the script, but to the HTML itself. Take
                these table elements from your page. They're from two different rows.

                <td width="55" rowspan="2">
                <div align="center">
                <input name="car1" type="text" class="text" id="autoReimb13 "
                size="8">
                </div>
                </td>

                and

                <td rowspan="2">
                <div align="center">
                <input name="car2" type="text" class="text" id="car1" size="8">
                </div>
                </td>

                The id of the second input element matches the name of the first element.
                When you go to look-up the value of the first input element with

                document.forms['reim_vouch'].elements['car1'].value

                the search will be conducted by id first, with names checked only if no
                match has been found. That means that you'll actually get the second input.

                Until you correct this, you can't have a working script. I assume that you
                just need to remove the id attributes, but I don't know if that will
                affect other parts of the page. You'll have to assess the impact.

                By the way, I'm not guaranteeing that changing the HTML will ensure
                success. The script might need altering afterwards, but first things
                first. If I could understand the page, I might be able to offer some
                assurances, but I'll be honest and say that it's a nightmare to navigate
                without some form of instruction.

                [snip]

                Mike

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

                Comment

                Working...