Form
elements such as checkboxes, radio buttons, and selection lists. You can also use a form to post data to a server.FORM
tag. The JavaScript runtime engine creates a Form
object for each FORM
tag in the document. You access FORM
objects through the document.forms
property and through named properties of that object.
To define a form, use standard HTML syntax with the addition of JavaScript event handlers. If you supply a value for the NAME
attribute, you can use that value to index into the forms
array. In addition, the associated document
object has a named property for each named form.
NAME
attribute) or the Form.elements
array. The elements
array contains an entry for each element (such as a Checkbox
, Radio
, or Text
object) in a form.
If multiple objects on the same form have the same NAME
attribute, an array of the given name is created automatically. Each element in the array represents an individual Form
object. Elements are indexed in source order starting at 0. For example, if two Text
elements and a Textarea
element on the same form have their NAME
attribute set to "myField"
, an array with the elements myField[0]
, myField[1]
, and myField[2]
is created. You need to be aware of this situation in your code and know whether myField
refers to a single element or to an array of elements.
Property |
Description
|
|
|
|
|
|
| |
---|
Method |
Description
|
Simulates a mouse click on a reset button for the calling form.
| |
---|
watch
and unwatch
methods from Object
.
myForm
that contains text fields for first name and last name. The form also contains two buttons that change the names to all uppercase or all lowercase. The function setCase
shows how to refer to the form by its name.
<HTML>
<HEAD>
<TITLE>Form object example</TITLE>
</HEAD>
<SCRIPT>
function setCase (caseSpec){
if (caseSpec == "upper") {
document.myForm.firstName.value=document.myForm.firstName.value.toUpperCase()
document.myForm.lastName.value=document.myForm.lastName.value.toUpperCase()}
else {
document.myForm.firstName.value=document.myForm.firstName.value.toLowerCase()
document.myForm.lastName.value=document.myForm.lastName.value.toLowerCase()}
}
</SCRIPT>
<BODY>Example 2: forms array. The
<FORM NAME="myForm">
<B>First name:</B>
<INPUT TYPE="text" NAME="firstName" SIZE=20>
<BR><B>Last name:</B>
<INPUT TYPE="text" NAME="lastName" SIZE=20>
<P><INPUT TYPE="button" VALUE="Names to uppercase" NAME="upperButton"
onClick="setCase('upper')">
<INPUT TYPE="button" VALUE="Names to lowercase" NAME="lowerButton"
onClick="setCase('lower')">
</FORM>
</BODY>
</HTML>
onLoad
event handler in the following example displays the name of the first form in an Alert dialog box.
<BODY onLoad="alert('You are looking at the ' + document.forms[0] + ' form!')">If the form name is
musicType
, the alert displays the following message:
You are looking at the <object musicType> form!Example 3: onSubmit event handler. The following example shows an
onSubmit
event handler that determines whether to submit a form. The form contains one Text
object where the user enters three characters. onSubmit
calls a function, checkData
, that returns true if there are 3 characters; otherwise, it returns false. Notice that the form's onSubmit
event handler, not the submit button's onClick
event handler, calls the checkData
function. Also, the onSubmit
handler contains a return
statement that returns the value obtained with the function call; this prevents the form from being submitted if invalid data is specified. See onSubmit
for more information.
<HTML>Example 4: submit method. The following example is similar to the previous one, except it submits the form using the
<HEAD>
<TITLE>Form object/onSubmit event handler example</TITLE>
<TITLE>Form object example</TITLE>
</HEAD>
<SCRIPT>
var dataOK=false
function checkData (){
if (document.myForm.threeChar.value.length == 3) {
return true}
else {
alert("Enter exactly three characters. " + document.myForm.threeChar.value +
" is not valid.")
return false}
}
</SCRIPT>
<BODY>
<FORM NAME="myForm" onSubmit="return checkData()">
<B>Enter 3 characters:</B>
<INPUT TYPE="text" NAME="threeChar" SIZE=3>
<P><INPUT TYPE="submit" VALUE="Done" NAME="submit1"
onClick="document.myForm.threeChar.value=document.myForm.threeChar.value.toUpperCase()">
</FORM>
</BODY>
</HTML>
submit
method instead of a Submit
object. The form's onSubmit
event handler does not prevent the form from being submitted. The form uses a button's onClick
event handler to call the checkData
function. If the value is valid, the checkData
function submits the form by calling the form's submit
method.
<HTML>
<HEAD>
<TITLE>Form object/submit method example</TITLE>
</HEAD>
<SCRIPT>
var dataOK=false
function checkData (){
if (document.myForm.threeChar.value.length == 3) {
document.myForm.submit()}
else {
alert("Enter exactly three characters. " + document.myForm.threeChar.value +
" is not valid.")
return false}
}
</SCRIPT>
<BODY>
<FORM NAME="myForm" onSubmit="alert('Form is being submitted.')">
<B>Enter 3 characters:</B>
<INPUT TYPE="text" NAME="threeChar" SIZE=3>
<P><INPUT TYPE="button" VALUE="Done" NAME="button1"
onClick="checkData()">
</FORM>
</BODY>
</HTML>
Button
, Checkbox
, FileUpload
, Hidden
, Password
, Radio
, Reset
, Select
, Submit
, Text
, Textarea
.
A string specifying a destination URL for form data that is submitted
mailto:
or news:
URL requires the UniversalSendMail
privilege. For information on security, see the Client-Side JavaScript Guide.
JavaScript 1.1.
This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.
action
property is a reflection of the ACTION
attribute of the FORM
tag. Each section of a URL contains different information. See Location
for a description of the URL components.
action
property of the musicForm
form to the value of the variable urlName:
document.musicForm.action=urlName
Form.encoding
, Form.method
, Form.target
checkbox
, radio
, and Text
objects) in source order.elements
array. This array contains an entry for each object (Button
, Checkbox
, FileUpload
, Hidden
, Password
, Radio
, Reset
, Select
, Submit
, Text
, or Textarea
object) in a form in source order. Each radio button in a Radio
object appears as a separate element in the elements
array. For example, if a form called myForm
has a text field and two checkboxes, you can refer to these elements myForm.elements[0]
, myForm.elements[1]
, and myForm.elements[2]
.
Although you can also refer to a form's elements by using the element's name (from the NAME
attribute), the elements
array provides a way to refer to Form
objects programmatically without using their names. For example, if the first object on the userInfo
form is the userName
Text
object, you can evaluate it in either of the following ways:
userInfo.userName.valueThe value of each element in the
userInfo.elements[0].value
elements
array is the full HTML statement for the object.
To obtain the number of elements in a form, use the length
property: myForm.elements.length
.
window
.
encoding
property initially reflects the ENCTYPE
attribute of the FORM
tag; however, setting encoding
overrides the ENCTYPE
attribute.
encoding
property of musicForm
:
function getEncoding() {
return document.musicForm.encoding
}
Form.action
, Form.method
, Form.target
handleEvent(event)
event | The name of an event for which the specified object has an event handler. |
form.length
property tells you how many elements are in the form. You can get the same information using form.elements.length
.
method
property is a reflection of the METHOD
attribute of the FORM
tag. The method
property should evaluate to either "get"
or "post"
.
musicForm
method
property:
function getMethod() {
return document.musicForm.method
}
Form.action
, Form.encoding
, Form.target
name
property initially reflects the value of the NAME
attribute. Changing the name
property overrides this setting.
valueGetter
function uses a for
loop to iterate over the array of elements on the valueTest
form. The msgWindow
window displays the names of all the elements on the form:
newWindow=window.open("http://home.netscape.com")
function valueGetter() {
var msgWindow=window.open("")
for (var i = 0; i < newWindow.document.valueTest.elements.length; i++) {
msgWindow.document.write(newWindow.document.valueTest.elements[i].name + "<BR>")
}
}
reset()
reset
method restores a form element's default values. A reset button does not need to be defined for the form.
Text
object in which the user is to type "CA" or "AZ". The Text
object's onChange
event handler calls a function that executes the form's reset
method if the user provides incorrect input. When the reset
method executes, defaults are restored and the form's onReset
event handler displays a message.
<SCRIPT>
function verifyInput(textObject) {
if (textObject.value == 'CA' || textObject.value == 'AZ') {
alert('Nice input')
}
else { document.myForm.reset() }
}
</SCRIPT>
<FORM NAME="myForm" onReset="alert('Please enter CA or AZ.')">
Enter CA or AZ:
<INPUT TYPE="text" NAME="state" SIZE="2" onChange=verifyInput(this)><P>
</FORM>
onReset
, Reset
submit()
mailto:
or news:
URL requires the UniversalSendMail
privilege. For information on security, see the Client-Side JavaScript Guide.
JavaScript 1.1: The submit
method fails without notice if the form's action
is a mailto:
, news:
, or snews:
URL. Users can submit forms with such URLs by clicking a submit button, but a confirming dialog will tell them that they are about to give away private or sensitive information.
submit
method submits the specified form. It performs the same action as a submit button.
Use the submit method to send data back to an HTTP server. The submit
method returns the data using either "get" or "post," as specified in Form.method
.
musicChoice
:
document.musicChoice.submit()If
musicChoice
is the first form created, you also can submit it as follows:
document.forms[0].submit()See also the example for
Form
.
Submit
, onSubmit
target
property initially reflects the TARGET
attribute of the A
, AREA
, and FORM
tags; however, setting target
overrides these attributes.
You can set target
using a string, if the string represents a window name. The target
property cannot be assigned the value of a JavaScript expression or variable.
musicInfo
form are displayed in the msgWindow
window:
document.musicInfo.target="msgWindow"
Form.action
, Form.encoding
, Form.method
Last Updated: 05/28/99 11:59:27