11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

name="myform">> no matter where it occurs in a document. <strong>The</strong> main properties of an<br />

individual Form (or under the DOM HTMLFormElement) object are related to the attributes of<br />

the tag and include<br />

action <strong>The</strong> URL to submit the form to as specified by the action attribute. If<br />

unspecified, the form will submit to the current document location.<br />

encoding <strong>The</strong> value of the enctype attribute, generally application/x-www-formurlencoded<br />

unless using a file upload when it should be multipart/form-data.<br />

Occasionally, value may be text/plain when using a mailto: URL submission.<br />

encType <strong>The</strong> DOM property to be used in place of the traditional encoding property to<br />

access the enctype attribute‘s value.<br />

method <strong>The</strong> method attribute value, either get or post. <strong>The</strong> get method is the default<br />

when unspecified.<br />

name <strong>The</strong> name of the form if defined.<br />

target <strong>The</strong> window or frame name to display the form result within.<br />

<strong>The</strong> Form object also specifies a length property that corresponds to the number of fields within<br />

the form defined by , , , and possibly in<br />

browsers that support this HTML element. Object references to these elements are stored in<br />

the elements[] collection of the Form object to be discussed next. Last, the Form object<br />

supports two methods, submit() and reset(), which correspond to the submission and resetting<br />

of the form.<br />

Form Elements Collection<br />

<strong>The</strong> elements[] collection for each Form object is an array containing the various fields in a<br />

form including checkboxes, radio buttons, select menus, text areas, text fields, password fields,<br />

Reset buttons, Submit buttons, generic buttons, and even hidden fields. Later <strong>JavaScript</strong><br />

implementations also support file upload fields. Access to form elements can be performed<br />

numerically (document.myform.elements[0]) or by name (document.myform.textfield1).<br />

<strong>The</strong> number of elements in the form is accessible either with document.formname.length or<br />

document.formname.elements.length. <strong>The</strong> properties of each form field object vary based<br />

upon the HTML syntax. Let‘s look at the standard text field to get the idea.<br />

A text field in (X)HTML is defined by , so<br />

accordingly you would expect the properties for a text field object to be type, name, size,<br />

maxlength, and value. In the case of the DOM standard, we see that the maxlength attribute<br />

should be referenced as maxLength. Also defined is the property defaultValue, which holds<br />

the original value, specified by the value attribute since the value property of this object will<br />

change as the user changes the field. A simple example showing the manipulation of a text field<br />

is shown here.<br />

<br />

<br />

<br />

Text Field Fun<br />

<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!