javascript and form names

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

    javascript and form names

    I have a script that selects a value in an option in a selection list
    according to how many letters were entered in an input box. I have it
    working but I am limited to what I can name the form fields because
    they are dynamically created by a PHP script.

    I can get it to work with simple name but not with names outputted by
    the script. Is there a change I can make to the JavaScript so I can
    use the dynamic names?

    Simple name = 'letter_number'
    Dynamic name = 'id[txt_3]'

    -----------------------------
    EX w/ simple input names
    WORKING:
    <form action="#" name="form_name ">
    Text Insert:
    <input type="text" name ="text_name" size="6" maxlength="6" value=""
    onChange="docum ent.form_name.l etter_number.va lue=document.fo rm_name.text_na me.value.length +6;"
    onBlur="documen t.form_name.let ter_number.valu e=document.form _name.text_name .value.length+6 ;">
    <br>
    # of letters:
    <select name="letter_nu mber">
    <option value="7">1</option>
    <option value="8">2</option>
    <option value="9">3 (+$4.00)</option>
    <option value="10">4 (+$8.00)</option>
    <option value="11">5 (+$12.00)</option>
    <option value="12">6 (+$16.00)</option>
    </select>
    </form>

    ---------------------------
    w/ outputted dynamic names (just need to get it working with this type
    of name)
    NON WORKING:
    <form action="#" name="form_name ">
    Text Insert:
    <input type="text" name ="id[txt_3]" size="6" maxlength="6" value=""
    onChange="docum ent.form_name.i d[5].value=document .form_name.id[txt_3].value.length+6 ;"
    onBlur="documen t.form_name.id[5].value=document .form_name.id[txt_3].value.length+6 ;">
    <br>
    # of letters:
    <select name="id[5]">
    <option value="7">1</option>
    <option value="8">2</option>
    <option value="9">3 (+$4.00)</option>
    <option value="10">4 (+$8.00)</option>
    <option value="11">5 (+$12.00)</option>
    <option value="12">6 (+$16.00)</option>
    </select>
    </form>
  • Lee

    #2
    Re: javascript and form names

    dan said:[color=blue]
    >
    >I have a script that selects a value in an option in a selection list
    >according to how many letters were entered in an input box. I have it
    >working but I am limited to what I can name the form fields because
    >they are dynamically created by a PHP script.
    >
    >I can get it to work with simple name but not with names outputted by
    >the script. Is there a change I can make to the JavaScript so I can
    >use the dynamic names?
    >
    >Simple name = 'letter_number'
    >Dynamic name = 'id[txt_3]'[/color]



    Comment

    • dan

      #3
      Re: javascript and form names

      Lee <REM0VElbspamtr ap@cox.net> wrote in message news:<bpik7d013 r3@drn.newsguy. com>...[color=blue]
      > dan said:[color=green]
      > >
      > >I have a script that selects a value in an option in a selection list
      > >according to how many letters were entered in an input box. I have it
      > >working but I am limited to what I can name the form fields because
      > >they are dynamically created by a PHP script.
      > >
      > >I can get it to work with simple name but not with names outputted by
      > >the script. Is there a change I can make to the JavaScript so I can
      > >use the dynamic names?
      > >
      > >Simple name = 'letter_number'
      > >Dynamic name = 'id[txt_3]'[/color]
      >
      > http://www.jibbering.com/faq/#FAQ4_25[/color]

      so for
      <input type="text" name ="id[txt_3]" size="6" maxlength="6" value="">
      or
      <input type="text" name ="id[5]" size="6" maxlength="6" value="">


      I can access it with:
      document.form_n ame.elements["id[txt_3]"].value.length
      or
      document.form_n ame.elements["id[5]"].value.length

      ???
      thanks
      ???

      Comment

      • Lee

        #4
        Re: javascript and form names

        dan said:[color=blue]
        >
        >Lee <REM0VElbspamtr ap@cox.net> wrote in message[color=green]
        >> http://www.jibbering.com/faq/#FAQ4_25[/color]
        >
        >so for
        > <input type="text" name ="id[txt_3]" size="6" maxlength="6" value="">
        >or
        > <input type="text" name ="id[5]" size="6" maxlength="6" value="">
        >
        >
        >I can access it with:
        >document.form_ name.elements["id[txt_3]"].value.length
        >or
        >document.form_ name.elements["id[5]"].value.length
        >
        >???[/color]

        Yes, and in less time than it took you to post this question,
        you could have developed and tested a simple test case to
        prove that to yourself:

        <html>
        <body onload="alert(d ocument.form_na me.elements['id[5]'].value)">
        <form name="form_name ">
        <input name="id[5]" value="it works">
        </form>
        </body>
        </html>

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: javascript and form names

          ryan000@yahoo.c om (dan) writes:
          [color=blue]
          > I can access it with:
          > document.form_n ame.elements["id[txt_3]"].value.length[/color]

          I prefer to use the forms collection. It is official W3C DOM, while
          having the form directly as a property of the document element isn't.

          document.forms["form_name"].elements["id[txt_3]".value.len gth
          [color=blue]
          > or
          > document.form_n ame.elements["id[5]"].value.length
          >
          > ???[/color]

          Try it! (but yes!)

          /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

            #6
            Re: javascript and form names

            dan wrote on 20 Nov 2003:
            [color=blue]
            > so for
            > <input type="text" name ="id[txt_3]" size="6" maxlength="6"
            > value="">
            > or
            > <input type="text" name ="id[5]" size="6" maxlength="6"
            > value="">
            >
            > I can access it with:
            > document.form_n ame.elements["id[txt_3]"].value.length
            > or
            > document.form_n ame.elements["id[5]"].value.length[/color]

            If "id[txt_3]" and "id[5]" are the names of the controls after being
            parsed by the PHP interpreter, yes. However, you should change them.
            The only valid characters in a control name are alphanumeric
            characters, hyphens (-), periods (.), underscores (_), and colons
            (:). Also, controls names must start with a letter.

            Mike

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

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: javascript and form names

              Michael Winter <M.Winter@bluey onder.co.uk.inv alid> writes:
              [color=blue]
              > The only valid characters in a control name are alphanumeric
              > characters, hyphens (-), periods (.), underscores (_), and colons
              > (:). Also, controls names must start with a letter.[/color]

              We had that discussion a while ago, and the conclusion was that
              there are not restrictions on the names of controls.

              In HTML 4, the value of the name attribute on input, textarea, select,
              object and button elements are CDATA (and not NAME). While the
              specification says:
              ---
              For some HTML 4 attributes with CDATA attribute values, the
              specification imposes further constraints on the set of legal values
              for the attribute that may not be expressed by the DTD.
              ---
              there are no such restrictions on the name attributes of form controls
              (i.e., control names).

              Control names and values are both escaped before sending as content
              type "applicatio n/x-www-form-urlencoded".

              Another quote from the specification (under "multipart/form-data"):
              ---
              Control names originally encoded in non-ASCII character sets may be
              encoded using the method outlined in [RFC2045].
              ---

              That means that FAQ entry 4.25 is incorrect.

              /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

                #8
                Re: javascript and form names

                Lasse Reichstein Nielsen wrote on 21 Nov 2003:
                [color=blue]
                > Michael Winter <M.Winter@bluey onder.co.uk.inv alid> writes:
                >[color=green]
                >> The only valid characters in a control name are alphanumeric
                >> characters, hyphens (-), periods (.), underscores (_), and
                >> colons (:). Also, controls names must start with a letter.[/color][/color]

                Withdrawn. It just seemed a sensible association: name attributes of
                NAME type.
                [color=blue]
                > We had that discussion a while ago, and the conclusion was that
                > there are not restrictions on the names of controls.[/color]

                No leading and trailing spaces is probably one.
                [color=blue]
                > In HTML 4, the value of the name attribute on input, textarea,
                > select, object and button elements are CDATA (and not NAME).
                > While the specification says:
                > ---
                > For some HTML 4 attributes with CDATA attribute values, the
                > specification imposes further constraints on the set of legal
                > values for the attribute that may not be expressed by the DTD.
                > ---
                > there are no such restrictions on the name attributes of form
                > controls (i.e., control names).
                >
                > Control names and values are both escaped before sending as
                > content type "applicatio n/x-www-form-urlencoded".
                >
                > Another quote from the specification (under
                > "multipart/form-data"): ---
                > Control names originally encoded in non-ASCII character sets
                > may be encoded using the method outlined in [RFC2045].
                > ---[/color]

                It would appear that the NAME type is only used for language codes,
                and the name and http-equiv attributes in META elements.

                Mike

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

                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: javascript and form names

                  Michael Winter <M.Winter@bluey onder.co.uk.inv alid> writes:
                  [color=blue]
                  > Lasse Reichstein Nielsen wrote on 21 Nov 2003:[/color]
                  [color=blue]
                  > Withdrawn. It just seemed a sensible association: name attributes of
                  > NAME type.[/color]

                  Most of the people in this grouped had thought the same thing. We were
                  quite surpriced :)
                  [color=blue][color=green]
                  >> We had that discussion a while ago, and the conclusion was that
                  >> there are not restrictions on the names of controls.[/color]
                  >
                  > No leading and trailing spaces is probably one.[/color]

                  True. It is only a recommendation, but since browsers are free to
                  choose whether to strip the whitespace, writing it is asking for
                  trouble.
                  [color=blue]
                  > It would appear that the NAME type is only used for language codes,
                  > and the name and http-equiv attributes in META elements.[/color]

                  Yes, that was my reading too.

                  For all other tags with name attributes, i.e., a, applet, form, frame,
                  iframe, img, and not control names, the specifiaction has this note:
                  ---
                  Note. This attribute has been included for backwards
                  compatibility. Applications should use the id attribute to identify
                  elements.
                  ---
                  Also, if you have both "id" and "name" attributes in the same
                  non-form-control tag, they must have identical values.
                  <URL:http://www.w3.org/TR/html4/struct/links.html#anch ors-with-id>

                  Still, if you omit the "id" attribute, you can still give your images
                  "name" attributes that are not valid NAMEs.

                  /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

                    #10
                    Re: javascript and form names

                    Lasse Reichstein Nielsen wrote on 21 Nov 2003:
                    [color=blue]
                    > Also, if you have both "id" and "name" attributes in the same
                    > non-form-control tag, they must have identical values.
                    > <URL:http://www.w3.org/TR/html4/struct/links.html#anch ors-with-id>[/color]

                    I hadn't noticed that. It seems odd to place such an important note
                    in such an inconspicuous place.

                    Mike

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

                    Comment

                    • Dr John Stockton

                      #11
                      Re: javascript and form names

                      JRS: In article <bpjeeg0u56@drn .newsguy.com>, seen in
                      news:comp.lang. javascript, Lee <REM0VElbspamtr ap@cox.net> posted at Thu,
                      20 Nov 2003 14:15:44 :-[color=blue]
                      >
                      >Yes, and in less time than it took you to post this question,
                      >you could have developed and tested a simple test case to
                      >prove that to yourself:[/color]

                      A test case, executed in one or a few browsers, can prove that something
                      does not work in all systems; thus, one should test before asking. But
                      that cannot prove that it does work in all systems.

                      Asking here has a good chance of getting a reply valid at least for
                      almost all systems; it has greater coverage.



                      Referring to the FAQ 4.25 reference elsewhere in the thread :

                      ISTM that the FAQ should recommend that, where practicable, authors
                      should choose names following the usual conventions for identifiers in
                      computer languages - characters chosen from alphanumeric and underscore,
                      first character not a digit, and they should choose as for case-
                      independence.

                      Such names are computer-safe, human-safe, and look like names.

                      But it may be necessary to use names not following the above; and the
                      FAQ should give methods for using those (as well as commenting on
                      legality).

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                      Comment

                      Working...