04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13: Scripting Forms<br />

If a name is in use by multiple form controls, as is the case with radio buttons, then a NodeList is<br />

returned containing all of the elements with the name. For example, consider the following HTML<br />

snippet:<br />

< form method=”post” id=”myForm” ><br />

< ul ><br />

< li > < input type=”radio” name=”color” value=”red” > Red < /li ><br />

< li > < input type=”radio” name=”color” value=”green” > Green < /li ><br />

< li > < input type=”radio” name=”color” value=”blue” > Blue < /li ><br />

< /ul ><br />

< /form ><br />

The form in this HTML has three radio controls that have “ color ” as their name , which ties the fields<br />

together. When accessing elements[ “ color “ ] , a NodeList is returned, containing all three elements;<br />

when accessing elements[0] , however, only the first element is returned. Consider this example:<br />

var form = document.getElementById(“myForm”);<br />

var colorFields = form.elements[“color”];<br />

alert(colorFields.length); //3<br />

var firstColorField = colorFields[0];<br />

var firstFormField = form.elements[0];<br />

alert(firstColorField === firstFormField);<br />

//true<br />

This code shows that the first form field, accessed via form.elements[0] , is the same as the first<br />

element contained in form.elements[ “ color “ ] .<br />

It ’ s possible to access elements as properties of a form as well, such as form[0] to get the first form field<br />

and form[ “ color “ ] to get a named field. These properties always return the same thing as their<br />

equivalent in the elements collection. This approach is provided for backwards compatibility with<br />

older browsers and should be avoided when possible in favor of using elements .<br />

Common Form - Field Properties<br />

With the exception of the < fieldset > element, all form fields share a common set of properties. Since<br />

the < input > type represents many form fields, some properties are used only with certain field types,<br />

whereas others are used regardless of the field type. The common form - field properties and methods are<br />

as follows:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

disabled — A Boolean indicating if the field is disabled.<br />

form — A pointer to the form that the field belongs to. This property is read only.<br />

name — The name of the field.<br />

readOnly — A Boolean indicating if the field is read only.<br />

tabIndex — Indicates the tab order for the field.<br />

type — The type of the field: “ checkbox ” , “ radio ” , and so on.<br />

value — The value of the field that will be submitted to the server. For file - input fields, this<br />

property is read only and simply contains the file ’ s path on the computer.<br />

437

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

Saved successfully!

Ooh no, something went wrong!