From: FAQ server on
-----------------------------------------------------------------------
FAQ Topic - How do I get the value of a form control?
-----------------------------------------------------------------------

In HTML documents, a form may be referred to as a property of the
`document.forms` collection, either by its ordinal index or by name
(if the `form` has a name). A `form`'s controls may be similarly referenced
from its `elements` collection:

var frm = document.forms[0];
var control = frm.elements["elementname"];

Once a reference to a control is obtained, its (string) `value`
property can be read:-

var value = control.value;
value = +control.value; //string to number.

Some exceptions would be:

First Exception: Where the control is a `SELECT` element, and
support for older browsers, such as NN4, is required:

var value = control.options[control.selectedIndex].value;

Second Exception: Where several controls share the same name,
such as radio buttons. These are made available as collections
and require additional handling. For more information, see:-

<URL: http://jibbering.com/faq/notes/form-access/>
Unsafe Names for HTML Form Controls [ref 1]

Third Exception: File inputs. Most current browsers do not allow
reading of `type="file"` input elements in a way that is useful.

References:
-----------

[1] http://jibbering.com/faq/names/


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

--

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

From: Dr J R Stockton on
In comp.lang.javascript message <4c5de57b$0$280$14726298(a)news.sunsite.dk
>, Sat, 7 Aug 2010 23:00:03, FAQ server <javascript(a)dotinternet.be>
posted:

>FAQ Topic - How do I get the value of a form control?

>In HTML documents, a form may be referred to as a property of the
>`document.forms` collection, either by its ordinal index or by name
>(if the `form` has a name).

There are at least two other ways if getting a reference to a form, not
counting perambulating the DOM structure from somewhere nearby.


> A `form`'s controls may be similarly referenced
>from its `elements` collection:
>
>var frm = document.forms[0];
>var control = frm.elements["elementname"];

var control = frm.elementname;

is simpler, where applicable (which is usually).



>First Exception: Where the control is a `SELECT` element, and
>support for older browsers, such as NN4, is required:

NN4 was superseded almost 10 years ago, according to
<http://en.wikipedia.org/wiki/Timeline_of_web_browsers>. Its usage
share must now be insignificant.

>var value = control.options[control.selectedIndex].value;
>
>Second Exception: Where several controls share the same name,
>such as radio buttons.

There, "such as" seems to call for mote than one example.


--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)