Current index of an array of form elements?

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

    Current index of an array of form elements?

    I am trying to find a way to implement something like:

    <FORM name='form1'><T ABLE>
    server scripting loop (php), generates X (a variable number) lines
    with the 3 fields {
    <TR><TD><inpu t type='text' name='kminicial[]'></TD>
    <TD><input type='text' name='kmfinal[]'
    onBlur='documen t.form1.kmrodad os[currentindex].value=document .form1.kmfinal[currentindex].value-
    document.form1. kminicial[currentindex].value'></TD>
    <TD><input type='text' name='kmrodados[]'></TD></TR>
    }
    </TABLE></FORM>

    Just a simple onBlur math with the first two fields of each line,
    putting the result on the third. The problem is the "currentind ex"
    value, I wasnt able to find how to get this value anywhere, is it
    possible to implement this way?

    Thanks in advance,
    Norman
  • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

    #2
    Re: Current index of an array of form elements?

    Norman escribió:
    I am trying to find a way to implement something like:
    >
    <FORM name='form1'><T ABLE>
    server scripting loop (php), generates X (a variable number) lines
    with the 3 fields {
    <TR><TD><inpu t type='text' name='kminicial[]'></TD>
    <TD><input type='text' name='kmfinal[]'
    onBlur='documen t.form1.kmrodad os[currentindex].value=document .form1.kmfinal[currentindex].value-
    document.form1. kminicial[currentindex].value'></TD>
    <TD><input type='text' name='kmrodados[]'></TD></TR>
    }
    </TABLE></FORM>
    >
    Just a simple onBlur math with the first two fields of each line,
    putting the result on the third. The problem is the "currentind ex"
    value, I wasnt able to find how to get this value anywhere, is it
    possible to implement this way?
    If you use PHP, why don't you do an extra step?

    <?php

    $rowid = 0;

    ?>

    ....

    <input type=" name="kmfinal[]"
    onblur="updateR esultByRowId(<? echo $rowid++; ?>)">


    Otherwise, I can only think of this:

    <input type=" name="kmfinal[]"
    onblur="updateR esultByRightFie ld(this)">


    function updateResultByR ightField(right Field){
    var currentForm = rightField.form ;

    /*
    * And then write code to find:
    * var leftField = ......
    * var resultField
    *
    * ... either going up from rightField or going
    * down from currentForm...
    */
    }


    One more idea is to asign events to the row but I still prefer option 1 :)


    --
    -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
    -- Mi sitio sobre programación web: http://bits.demogracia.com
    -- Mi web de humor al baño María: http://www.demogracia.com
    --

    Comment

    • Norman

      #3
      Re: Current index of an array of form elements?

      On 8 maio, 12:02, "Álvaro G. Vicario"
      <alvaroNOSPAMTH A...@demogracia .comwrote:
      Norman escribió:
      >
      >
      >
      I am trying to find a way to implement something like:
      >
      <FORM name='form1'><T ABLE>
      server scripting loop (php), generates X (a variable number) lines
      with the 3 fields {
      <TR><TD><inpu t type='text' name='kminicial[]'></TD>
      <TD><input type='text' name='kmfinal[]'
      onBlur='documen t.form1.kmrodad os[currentindex].value=document .form1.kmfinal[currentindex].value-
      document.form1. kminicial[currentindex].value'></TD>
      <TD><input type='text' name='kmrodados[]'></TD></TR>
      }
      </TABLE></FORM>
      >
      Just a simple onBlur math with the first two fields of each line,
      putting the result on the third. The problem is the "currentind ex"
      value, I wasnt able to find how to get this value anywhere, is it
      possible to implement this way?
      >
      If you use PHP, why don't you do an extra step?
      >
      <?php
      >
      $rowid = 0;
      >
      ?>
      >
      ...
      >
      <input type=" name="kmfinal[]"
      onblur="updateR esultByRowId(<? echo $rowid++; ?>)">
      >
      Otherwise, I can only think of this:
      >
      <input type=" name="kmfinal[]"
      onblur="updateR esultByRightFie ld(this)">
      >
      function updateResultByR ightField(right Field){
      var currentForm = rightField.form ;
      >
      /*
      * And then write code to find:
      * var leftField = ......
      * var resultField
      *
      * ... either going up from rightField or going
      * down from currentForm...
      */
      >
      }
      >
      One more idea is to asign events to the row but I still prefer option 1 :)
      >
      --
      --http://alvaro.es- Álvaro G. Vicario - Burgos, Spain
      -- Mi sitio sobre programación web:http://bits.demogracia.com
      -- Mi web de humor al baño María:http://www.demogracia.com
      --
      Thanks Alvaro, I tried your 1st approach:

      <SCRIPT LANGUAGE=\"Java Script\">
      function calcula(indice) {
      document.formul ario.kmrodados[indice].value =
      document.formul ario.kmfinal[indice]-
      document.formul ario.kminicial[indice];
      return true;
      }
      </SCRIPT>

      and later, initialize the $indiceatual=0 on php, and inside the loop:

      <TD><input type='text' name='kminicial[]'>
      <TD><input type='text' name='kmfinal[]' onBlur='calcula (" .
      $indiceatual++ . ");'>
      <TD><input type='text' name='kmrodados[]'>

      but this generates an javascript error stating:

      this.document.f ormulario.kmrod ados has no properties

      on the 1st line of the calcula() function. The form name is declared
      as 'formulario'. Can you see what Im doing wrong here?

      Thank in advance,
      Norman

      Comment

      • Norman

        #4
        Re: Current index of an array of form elements?

        Solved. Hope this helps anyone: It was just a matter of referencing
        the input fields using the elements object in the javascript function:

        document.formul ario.elements['kmrodados[]']
        [indice].value=document .formulario.ele ments['kmfinal[]'][indice].value-
        document.formul ario.elements['kminicial[]'][indice].value;

        Norman

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Current index of an array of form elements?

          Norman wrote:
          Solved. Hope this helps anyone: It was just a matter of referencing
          the input fields using the elements object in the javascript function:
          >
          document.formul ario.elements['kmrodados[]']
          [indice].value=document .formulario.ele ments['kmfinal[]'][indice].value-
          document.formul ario.elements['kminicial[]'][indice].value;
          Should be

          var es = document.forms['formulario'].elements;
          ...
          es['kmrodados[]'][indice].value =
          es['kmfinal[]'][indice].value - es['kminicial[]'][indice].value;

          instead. Probably you don't need document.forms['formulario'] and the
          associated form name anyway, because you can pass the form object reference
          from an included form control with `this.form', and from the `form' element
          with `this'.

          Also note that the `-' operation performs only a simple conversion (from
          string) to number, which may lead to unexpected results e.g. with "0x42",
          "077", or "078". You may want to use parseInt(..., 10) or parseFloat(...)
          instead.


          PointedEars
          --
          Prototype.js was written by people who don't know javascript for people
          who don't know javascript. People who don't know javascript are not
          the best source of advice on designing systems that use javascript.
          -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

          Comment

          Working...