please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 17
Forms and Form Elements
Next
 

17.5 Naming Forms and Form Elements

As we saw above, all form elements have a NAME attribute that must be set in their HTML tags, if the form is to be submitted to a CGI script. While form submission is not generally of interest to JavaScript programs, there is another useful reason to specify this NAME tag; we'll explain it below.

The <FORM> tag also has a NAME attribute that you can set. This attribute has nothing to do with form submission. It exists for the convenience of JavaScript programmers. If the NAME attribute is defined in a <FORM> tag, then when the Form object is created for that form, it will be stored as an element in the forms[] array of the Document object, as usual, but it will also be stored in its own personal property of the Document object. The name of this newly defined property is the value of the NAME attribute. Thus, if you define a form with HTML like this:

<FORM NAME="questionnaire">
   ...
</FORM>
Then you can refer to that form as:

document.questionnaire
Often, you'll find this more convenient than the array notation:

document.forms[2]

Note that the <IMG>, <APPLET>, and <EMBED> tags all also have NAME attributes that work the same way as the NAME attribute of <FORM>. But with forms, this style of naming goes a step further, because all of the elements contained within a form have NAME attributes. When you give a form element a NAME attribute, you create a new property of the Form object that refers to that element. The name of this property is the value of the attribute, of course. Thus, you can refer to an element named "zipcode" in a form named "address" as:

document.address.zipcode
With reasonably chosen names, this syntax is much more elegant than the alternative which relies on hard-coded array indices:

document.forms[1].elements[4]

In HTML forms that use Checkbox and Radio elements, it is common practice to give each of a set of related elements the same name. For example, if a form contains a number Radio buttons that allow the user to indicate their favorite web browser, then each of these buttons might be given the name "favorite". The VALUE property of one button might be "nn", and the value of another might be "ie". When the form is submitted, a string like "favorite=mosaic" will be sent to indicate the user's selection. Using the same name for multiple elements is not a problem in this case because only one of those elements can be selected at a time, so only one value can be submitted with that name.

When more than one element in a form has the same NAME attribute, JavaScript simply places those elements into an array using the specified name. So, if the Radio objects in the example above were part of our form named "questionnaire", then you could refer to them with expressions like these:

document.questionnaire.favorite[0]
document.questionnaire.favorite[1]


Previous Home Next
Form Element Names and Values Book Index Form Verification Example

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/ch17_05.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/12/28 23:26,
TOP NEW HELP FIND: 
3.144.227.187:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://sxlist.com/TECHREF/language/java/script/definitive/ch17_05.htm"> [Chapter 17] 17.5 Naming Forms and Form Elements</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .