Addressing form elements using array

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

    #1

    Addressing form elements using array

    Hi,

    im using Struts with nested tags for form validation.
    So I end up with form elements named like this

    risikenManuell[0].praemieNeu
    risikenManuell[1].praemieNeu
    ....

    I would like to do a calculation on the input values
    using javascript but I don't know how to
    address the fields.

    With alert(document. vertragEditForm .risikenManuell[0].praemieNeu);
    I get the error, that this element is null or not an object.

    Is there a way to do it.

    TIA
    Frank
  • Lee

    #2
    Re: Addressing form elements using array

    Frank Stephan said:
    [color=blue]
    >With alert(document. vertragEditForm .risikenManuell[0].praemieNeu);
    >I get the error, that this element is null or not an object.
    >
    >Is there a way to do it.[/color]



    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Addressing form elements using array

      frank_stephan@g mx.de (Frank Stephan) writes:
      [color=blue]
      > So I end up with form elements named like this
      >
      > risikenManuell[0].praemieNeu[/color]
      ....[color=blue]
      > With alert(document. vertragEditForm .risikenManuell[0].praemieNeu);
      > I get the error, that this element is null or not an object.
      >
      > Is there a way to do it.[/color]

      Yes, and it's even in the FAQ:
      <URL:http://jibbering.com/faq/#FAQ4_25>
      In your case:
      document.forms['vertragEditFor m'].elements['riskenManuell[0].praemieNeu']

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Michael Winter

        #4
        Re: Addressing form elements using array

        On 7 Jan 2004 09:05:25 -0800, Frank Stephan <frank_stephan@ gmx.de> wrote:
        [color=blue]
        > Hi,
        >
        > im using Struts with nested tags for form validation.
        > So I end up with form elements named like this
        >
        > risikenManuell[0].praemieNeu
        > risikenManuell[1].praemieNeu
        > ...[/color]

        You mean like: <INPUT ... name="risikenMa nuell[0]">
        [color=blue]
        > I would like to do a calculation on the input values
        > using javascript but I don't know how to
        > address the fields.
        >
        > With alert(document. vertragEditForm .risikenManuell[0].praemieNeu);[/color]

        An style comment: you should use fully-qualified references to help
        prevent name collisions. That is, window.alert(.. .), not alert(...).
        [color=blue]
        > I get the error, that this element is null or not an object.[/color]

        The problem with your line above is that brackets ([ ]) are operators,
        used to subscript arrays. The JavaScript engine will interpret the
        risikenManuell[0] above as 'evaluate the first element of the array,
        risikenManuell' . To get around this, and other cross-browser problems, use
        the collections syntax:

        document.forms['form_name'].elements['control_name'].method_or_prop erty

        For example,

        document.forms['vertragEditFor m'].elements['risikenManuell[0]'].praemieNeu

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • HikksNotAtHome

          #5
          Re: Addressing form elements using array

          In article <opr1e348ws5vkl cq@news-text.blueyonder .co.uk>, Michael Winter
          <M.Winter@bluey onder.co.invali d> writes:
          [color=blue][color=green]
          >> With alert(document. vertragEditForm .risikenManuell[0].praemieNeu);[/color]
          >
          >An style comment: you should use fully-qualified references to help
          >prevent name collisions. That is, window.alert(.. .), not alert(...).[/color]

          Is there a browser that will alert when using window.alert(.. .) but *not alert*
          when using alert(...) ??

          If there isn't, then it seems superfluous.
          --
          Randy

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Addressing form elements using array

            hikksnotathome@ aol.com (HikksNotAtHome ) writes:
            [color=blue]
            > Is there a browser that will alert when using window.alert(.. .) but
            > *not alert* when using alert(...) ??[/color]

            Only if you have created another variable in scope with the name
            "alert". However, symmetrically, "window.ale rt" will fail if you have
            another variable called "window" in scope.

            I don't see any argument for writing "window." in front of global
            variables. I do it in *one* case: in front of "window.ope n" mainly for
            readability. It clearly distinguishes it from "document.open" .

            Otherwise I just write "alert", just as I write "parseFloat ", "Object"
            and "Array" ... and "window". There is no difference between these,
            they are all just global variables (i.e., properties of the global
            object).

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • HikksNotAtHome

              #7
              Re: Addressing form elements using array

              In article <ekubnwpd.fsf@h otpop.com>, Lasse Reichstein Nielsen <lrn@hotpop.com >
              writes:
              [color=blue]
              >hikksnotathome @aol.com (HikksNotAtHome ) writes:
              >[color=green]
              >> Is there a browser that will alert when using window.alert(.. .) but
              >> *not alert* when using alert(...) ??[/color]
              >
              >Only if you have created another variable in scope with the name
              >"alert". However, symmetrically, "window.ale rt" will fail if you have
              >another variable called "window" in scope.[/color]

              I already knew that, I wanted to see if he knew it. And my reply seemed more
              polite than a "Thats utterly false"
              --
              Randy

              Comment

              • Michael Winter

                #8
                Re: Addressing form elements using array

                On 07 Jan 2004 23:39:56 GMT, HikksNotAtHome <hikksnotathome @aol.com> wrote:
                [color=blue]
                > In article <opr1e348ws5vkl cq@news-text.blueyonder .co.uk>, Michael Winter
                > <M.Winter@bluey onder.co.invali d> writes:
                >[color=green]
                >> An style comment: you should use fully-qualified references to help
                >> prevent name collisions. That is, window.alert(.. .), not alert(...).[/color]
                >
                > Is there a browser that will alert when using window.alert(.. .) but *not
                > alert*
                > when using alert(...) ??
                >
                > If there isn't, then it seems superfluous.[/color]

                Read what I said again: "...to help prevent name collisions." I said
                nothing about browsers not accepting alert(...). Furthermore, I said that
                it was a style comment - therefore is was subjective, optional, and had no
                direct relationship to the solution.

                I take Mr Nielsen's point about the possibility of failure when a
                variable, window, is present in the same scope. However, I would think
                that no-one would create such a variable when accessing a window property
                or method. Especially when there would be no need to call it 'window', and
                a better, more descriptive name probably exists* (newWindow, popupWindow,
                helpWindow, etc.)

                Mike


                * The first two examples would only be 'better' names if used soon after
                the creation of a window and only for a short time.

                --
                Michael Winter
                M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                Comment

                • Laurent Bugnion, GalaSoft

                  #9
                  Re: Addressing form elements using array

                  Hi,

                  Michael Winter wrote:
                  [color=blue]
                  > Read what I said again: "...to help prevent name collisions." I said
                  > nothing about browsers not accepting alert(...). Furthermore, I said
                  > that it was a style comment - therefore is was subjective, optional, and
                  > had no direct relationship to the solution.
                  >
                  > I take Mr Nielsen's point about the possibility of failure when a
                  > variable, window, is present in the same scope. However, I would think
                  > that no-one would create such a variable when accessing a window
                  > property or method. Especially when there would be no need to call it
                  > 'window', and a better, more descriptive name probably exists*
                  > (newWindow, popupWindow, helpWindow, etc.)
                  >
                  > Mike
                  >
                  >
                  > * The first two examples would only be 'better' names if used soon after
                  > the creation of a window and only for a short time.
                  >[/color]

                  Which brings us to the next question: Who would be silly enough to use
                  "alert" as a global variable, resp. function name? The risk of name
                  collision exists in both cases. Concretely, there is nothing which
                  speaks in favour of using window.alert() instead of alert(), and there
                  is at least one argument against it: More code is sent to the client
                  which increases the file size ;-)

                  Laurent
                  --
                  Laurent Bugnion, GalaSoft
                  Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
                  Private/Malaysia: http://mypage.bluewin.ch/lbugnion
                  Support children in Calcutta: http://www.calcutta-espoir.ch

                  Comment

                  • Michael Winter

                    #10
                    Qualifying identifiers (was: Re: Addressing form elements using array)

                    On Thu, 08 Jan 2004 08:39:07 +0100, Laurent Bugnion, GalaSoft
                    <galasoft-LB@bluewin_NO_S PAM.ch> wrote:
                    [color=blue]
                    > Which brings us to the next question: Who would be silly enough to use
                    > "alert" as a global variable, resp. function name? The risk of name
                    > collision exists in both cases. Concretely, there is nothing which
                    > speaks in favour of using window.alert() instead of alert(), and there
                    > is at least one argument against it: More code is sent to the client
                    > which increases the file size ;-)[/color]

                    I would argue in favour of consistency. One would qualify references to
                    methods and properties, such as open() and close(), to distinguish between
                    the window and document members. Why not do it all the time? Why make
                    special cases?

                    Anyway, I already said that this was a matter of personal preference, so
                    let's not get hung-up on it, shall we?

                    Mike

                    --
                    Michael Winter
                    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                    Comment

                    • Richard Cornford

                      #11
                      Re: Addressing form elements using array

                      "Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_S PAM.ch> wrote in
                      message news:btj1cf$rbn $1@rex.ip-plus.net...
                      <snip>[color=blue]
                      >... and there is at least one argument against it:
                      >More code is sent to the client which increases the
                      >file size ;-)[/color]

                      To which it could be added that the qualified reference will take
                      fractionally longer to resolve.

                      There is a context in which it probably is advisable to (at leas
                      sometimes) provide more qualified references, and it is because of the
                      scope considerations that Lasse mentioned. The strings provided as
                      values of event handling attributes are used as the function bodies of
                      internally generated function objects assigned as methods of the
                      corresponding HTML elements. And some browsers provide those functions
                      with custom scope handling features that effectively add various objects
                      from the DOM to the scope chain of the function. One object that is
                      commonly added to such a scope chain is the - document - so a call to -
                      open() - may be resolved as - document.open - or as - window.open -
                      depending on whether the browser places the - document - in the scope
                      chain or not.

                      Another context where qualifying a global property with - window - (or
                      some similar property that refers to the global object) is when
                      performing type-converting test on global properties:-

                      if(location){
                      ...
                      }

                      would usually produce an error if - location - was undefined, while:-

                      if(window.locat ion){
                      ...
                      }

                      - will happily type-convert to true/false regardless. Though:-

                      if(typeof location != 'undefined'){
                      ...
                      }

                      - does not produce an error even if - location - is undefined (but it is
                      a slower operation).

                      I don't think that I would ever bother qualifying - alert("xxx"); - (but
                      alert is more of a debugging tool anyway so there is maybe no need to
                      worry about the validity of the assumption that it exists in the
                      environment).

                      Richard.


                      Comment

                      • Lasse Reichstein Nielsen

                        #12
                        Re: Addressing form elements using array

                        Michael Winter <M.Winter@bluey onder.co.invali d> writes:

                        [variable called "window"][color=blue]
                        > However, I would think that no-one would create such a variable when
                        > accessing a window property or method.[/color]

                        While I would probably not make one, thinking about it I believe that
                        I would be more likely to make a variable called "window" than one
                        called "alert". I often need to name the return value of window.open,
                        and while "newWindow" is better, it's also longer. But that's probably
                        why it's usually just "w".
                        /L
                        --
                        Lasse Reichstein Nielsen - lrn@hotpop.com
                        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                        'Faith without judgement merely degrades the spirit divine.'

                        Comment

                        • Lasse Reichstein Nielsen

                          #13
                          Re: Qualifying identifiers

                          Michael Winter <M.Winter@bluey onder.co.invali d> writes:
                          [color=blue]
                          > I would argue in favour of consistency. One would qualify references
                          > to methods and properties, such as open() and close(), to distinguish
                          > between the window and document members. Why not do it all the time?
                          > Why make special cases?[/color]

                          Are you planning on writing:
                          window.eval, window.parseFlo at, new window.Object, new window.Function ,
                          window.document .body, and window.document .getElementById ? Or event
                          window.window :)

                          And why "window", why not "self", which is a reference to the same object?
                          [color=blue]
                          > Anyway, I already said that this was a matter of personal preference,
                          > so let's not get hung-up on it, shall we?[/color]

                          Arh, you no phun! :P
                          /L
                          --
                          Lasse Reichstein Nielsen - lrn@hotpop.com
                          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                          'Faith without judgement merely degrades the spirit divine.'

                          Comment

                          Working...