99Chapter 9Forms and Form ElementsAccessing (Web host) form propertiesForms are
99Chapter 9Forms and Form ElementsAccessing form propertiesForms are created entirely from standard HTML tags in the page. You can set attributes forname, target, action, method, and enctype. Each of these is a property of a form object, accessed by all lowercase versions of those words, as indocument.forms[0].actiondocument.formName.actionTo change any of these properties, simply assign new values to them: document.forms[0].action = http://www.giantco.com/cgi/login.pl ; form.elements[] propertyIn addition to keeping track of each type of element inside a form, the browser also maintainsa list of all control elements within a form. This list is another array, with items listed accord- ing to the order in which their HTML tags appear in the source code. It is generally more effi- cient to create references to elements directly, using their names. However, sometimes ascript needs to look through all of the elements in a form. This is especially true if the contentof a form changes with each loading of the page because the number of text fields changesbased on the user s browser type. (For example, a script on the page uses document.write() to add an extra text box for information required only from Windows users.) The following code fragment shows the form.elements[]property at work in a forrepeatloop that looks at every element in a form to set the contents of text fields to an empty string. The script cannot simply barge through the form and set every element s content to an emptystring because some elements may be types (for example, a button) whose valuepropertieshave different purposes. var form = window.document.forms[0]; for (var i = 0; i < form.elements.length; i++) { if (form.elements[i].type == text ) { form.elements[i].value = ; } } In the first statement, I create a variable form that holds a reference to the first form ofthe document. I do this so that when I make many references to form elements later in thescript, the typical length of each reference is much shorter (and marginally faster). I can usethe formvariable as a shortcut to building references to items more deeply nested in theform. Next, I start looping through the items in the elementsarray for the form. Each form elementhas a typeproperty, which reveals what kind of form control it is: text, button, radio, check- box, and so on. I m interested in finding elements whose type is text. For each of those, I setthe valueproperty to an empty string. I return to forms later in this chapter to show you how to submit a form without a Submit but- ton and how client-side form validation works.
Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services