javascript and form names

Discussion in 'Javascript' started by dan, Nov 20, 2003.

  1. dan

    dan Guest

    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="document.form_name.letter_number.value=document.form_name.text_name.value.length+6;"
    onBlur="document.form_name.letter_number.value=document.form_name.text_name.value.length+6;">
    <br>
    # of letters:
    <select name="letter_number">
    <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="document.form_name.id[5].value=document.form_name.id[txt_3].value.length+6;"
    onBlur="document.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>
     
    dan, Nov 20, 2003
    #1
    1. Advertising

  2. dan

    Lee Guest

    dan said:
    >
    >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]'


    http://www.jibbering.com/faq/#FAQ4_25
     
    Lee, Nov 20, 2003
    #2
    1. Advertising

  3. dan

    dan Guest

    Lee <> wrote in message news:<>...
    > dan said:
    > >
    > >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]'

    >
    > http://www.jibbering.com/faq/#FAQ4_25


    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

    ???
    thanks
    ???
     
    dan, Nov 20, 2003
    #3
  4. dan

    Lee Guest

    dan said:
    >
    >Lee <> wrote in message
    >> http://www.jibbering.com/faq/#FAQ4_25

    >
    >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
    >
    >???


    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(document.form_name.elements['id[5]'].value)">
    <form name="form_name">
    <input name="id[5]" value="it works">
    </form>
    </body>
    </html>
     
    Lee, Nov 20, 2003
    #4
  5. (dan) writes:

    > I can access it with:
    > document.form_name.elements["id[txt_3]"].value.length


    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.length

    > or
    > document.form_name.elements["id[5]"].value.length
    >
    > ???


    Try it! (but yes!)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 20, 2003
    #5
  6. dan wrote on 20 Nov 2003:

    > 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


    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
    d (remove ".invalid" to reply)
     
    Michael Winter, Nov 20, 2003
    #6
  7. Michael Winter <> writes:

    > The only valid characters in a control name are alphanumeric
    > characters, hyphens (-), periods (.), underscores (_), and colons
    > :)). Also, controls names must start with a letter.


    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 "application/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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 21, 2003
    #7
  8. Lasse Reichstein Nielsen wrote on 21 Nov 2003:

    > Michael Winter <> writes:
    >
    >> The only valid characters in a control name are alphanumeric
    >> characters, hyphens (-), periods (.), underscores (_), and
    >> colons :)). Also, controls names must start with a letter.


    Withdrawn. It just seemed a sensible association: name attributes of
    NAME type.

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


    No leading and trailing spaces is probably one.

    > 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 "application/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].
    > ---


    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
    d (remove ".invalid" to reply)
     
    Michael Winter, Nov 21, 2003
    #8
  9. Michael Winter <> writes:

    > Lasse Reichstein Nielsen wrote on 21 Nov 2003:


    > Withdrawn. It just seemed a sensible association: name attributes of
    > NAME type.


    Most of the people in this grouped had thought the same thing. We were
    quite surpriced :)

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

    >
    > No leading and trailing spaces is probably one.


    True. It is only a recommendation, but since browsers are free to
    choose whether to strip the whitespace, writing it is asking for
    trouble.

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


    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#anchors-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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 21, 2003
    #9
  10. JRS: In article <>, seen in
    news:comp.lang.javascript, Lee <> posted at Thu,
    20 Nov 2003 14:15:44 :-
    >
    >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:


    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.demon.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
     
    Dr John Stockton, Nov 21, 2003
    #10
  11. Lasse Reichstein Nielsen wrote on 21 Nov 2003:

    > 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#anchors-with-id>


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

    Mike

    --
    Michael Winter
    d (remove ".invalid" to reply)
     
    Michael Winter, Nov 21, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jukka K. Korpela
    Replies:
    3
    Views:
    775
    Michael Winter
    Mar 6, 2004
  2. Michael Winter
    Replies:
    4
    Views:
    590
    Lasse Reichstein Nielsen
    Mar 6, 2004
  3. Robert
    Replies:
    0
    Views:
    511
    Robert
    Mar 6, 2004
  4. fBechmann
    Replies:
    0
    Views:
    400
    fBechmann
    Jun 10, 2004
  5. wanwan
    Replies:
    3
    Views:
    432
    Alex Martelli
    Oct 14, 2005
Loading...

Share This Page