Web server setup - 100Part IIJavaScript TutorialForm Controls as ObjectsThree kinds of

100Part IIJavaScript TutorialForm Controls as ObjectsThree kinds of HTML elements nested inside a

tag become scriptable objects in allbrowser document object models. Most of the objects owe their existence to the tagin the page s source code. Only the value assigned to the typeattribute of an tagdetermines whether the element is a text box, password entry field, hidden field, button, checkbox, or radio button. The other two kinds of form controls, textareaand select, havetheir own tags. To reference a particular form control as an object in DOM Level 0 syntax, you build thereference as a hierarchy starting with the document, through the form, and then to the con- trol. You ve already seen how many ways you can reference merely the form part all ofwhich are valid for building form control references. But if you were using only the identifiersassigned to the form and form control elements (that is, none of the associated arrays ofelements), the syntax is as follows: document.formName.controlNameFor example, consider the following simple form:

The following sample references to the text input control are all valid: document.searchForm.entrydocument.searchForm.elements[0] document.forms[ searchForm ].elements[ entry ] document.forms[ searchForm ].entryWhile form controls have several properties in common, some properties are unique to a par- ticular control type or related types. For example, only a selectobject offers a property thatreveals which item in its list is currently selected. But checkboxes and radio buttons bothhave a property that indicates whether the control is currently set to on. Similarly, all text- oriented controls operate the same way for reading and modifying their content. Having a good grasp of the scriptable features of form control objects is important to yoursuccess with JavaScript. In the next sections, you meet the most important form controlobjects and see how scripts interact with them. Text-related objectsEach of the four text-related HTML form elements inputelements of the text, password, and hidden types, plus the textareaelement is an element in the document object hierar- chy. All but the hidden object display themselves in the page, enabling users to enter infor- mation. These objects also display text information that changes in the course of using a page(although browsers capable of modern Dynamic HTML also allow the scripted change ofother body text in a document). To make these form control objects scriptable in a page, you do nothing special to their nor- mal HTML tags with the possible exception of assigning a nameattribute. I strongly recom- mend assigning unique names to every text-related form control element if your scripts willbe getting or setting properties or invoking their methods. Besides, if the form is actually sub- mitted to a server program, the nameattributes must be assigned in order for the server toreceive the element s data.

Leave a Reply