javascript and form names

D

dan

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

Lee

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
 
D

dan

Lee said:
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
???
 
L

Lee

dan said:
Lee said:

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

Lasse Reichstein Nielsen

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
 
M

Michael Winter

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
 
L

Lasse Reichstein Nielsen

Michael Winter said:
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"):
 
M

Michael Winter

Lasse Reichstein Nielsen wrote on 21 Nov 2003:

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
 
L

Lasse Reichstein Nielsen

Michael Winter said:
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 :)
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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top