Connecting Tech Pros Worldwide Help | Site Map

javascript and form names

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 12:00 PM
dan
Guest
 
Posts: n/a
Default 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="document.form_name.letter_number.value=d ocument.form_name.text_name.value.length+6;"
onBlur="document.form_name.letter_number.value=doc ument.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>

  #2  
Old July 20th, 2005, 12:00 PM
Lee
Guest
 
Posts: n/a
Default 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]

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

  #3  
Old July 20th, 2005, 12:00 PM
dan
Guest
 
Posts: n/a
Default Re: javascript and form names

Lee <REM0VElbspamtrap@cox.net> wrote in message news:<bpik7d013r3@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_name.elements["id[txt_3]"].value.length
or
document.form_name.elements["id[5]"].value.length

???
thanks
???
  #4  
Old July 20th, 2005, 12:01 PM
Lee
Guest
 
Posts: n/a
Default Re: javascript and form names

dan said:[color=blue]
>
>Lee <REM0VElbspamtrap@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(document.form_name.elements['id[5]'].value)">
<form name="form_name">
<input name="id[5]" value="it works">
</form>
</body>
</html>

  #5  
Old July 20th, 2005, 12:01 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: javascript and form names

ryan000@yahoo.com (dan) writes:
[color=blue]
> I can access it with:
> document.form_name.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.length
[color=blue]
> or
> document.form_name.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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #6  
Old July 20th, 2005, 12:01 PM
Michael Winter
Guest
 
Posts: n/a
Default 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_name.elements["id[txt_3]"].value.length
> or
> document.form_name.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@blueyonder.co.uk.invalid (remove ".invalid" to reply)
  #7  
Old July 20th, 2005, 12:01 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: javascript and form names

Michael Winter <M.Winter@blueyonder.co.uk.invalid> 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 "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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #8  
Old July 20th, 2005, 12:01 PM
Michael Winter
Guest
 
Posts: n/a
Default Re: javascript and form names

Lasse Reichstein Nielsen wrote on 21 Nov 2003:
[color=blue]
> Michael Winter <M.Winter@blueyonder.co.uk.invalid> 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 "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].
> ---[/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@blueyonder.co.uk.invalid (remove ".invalid" to reply)
  #9  
Old July 20th, 2005, 12:01 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: javascript and form names

Michael Winter <M.Winter@blueyonder.co.uk.invalid> 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#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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #10  
Old July 20th, 2005, 12:01 PM
Michael Winter
Guest
 
Posts: n/a
Default 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#anchors-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@blueyonder.co.uk.invalid (remove ".invalid" to reply)
  #11  
Old July 20th, 2005, 12:01 PM
Dr John Stockton
Guest
 
Posts: n/a
Default Re: javascript and form names

JRS: In article <bpjeeg0u56@drn.newsguy.com>, seen in
news:comp.lang.javascript, Lee <REM0VElbspamtrap@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.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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.