Every Web page containing forms exposes a document.forms collection, which is an array of forms on the page. Each <form> block in the page can be therefore referred to by number, where document.forms[0] refers to the first <form> block on the page, document.forms[1] refers to the second form, and so on. The number of forms is always available as document.forms.length. If a <form> tag includes a name attribute, you can also refer to it by name as a property of the document object. So, if a page includes a single form that has a name="SignupForm" attribute, you can refer to the form as either document.forms[0] or document.SignupForm. Each form element (text inputs, select boxes, checkboxes, and so on) within a form can be referred to by name. So, if a form named SignupForm contains a text input field with a name="FirstName" attribute, then that element can be referred to as document.forms[0].FirstName or document. SignupForm.FirstName. Table 17.10 shows the most important properties available for controlling each type of form element via JavaScript, where "most important" means the properties you need to use to obtain or change the element's value. In this table, element refers to the element object, so you would replace element with document.forms[0].elementname, where elementname corresponds to the NAME attribute of the <input>, <select>, or <textarea> element in question.
NOTE Always remember that JavaScript is case-sensitive, so if you have an <input> with a name="FirstName" attribute, you can replace element in this table with document.forms[0].FirstName but not document.forms[0].firstname or Document.Forms[0].Firstname. So, if a page contains a single form with a name attribute of SignupForm, and the form contains a text field called FirstName and a drop-down list (select box) called CCType, then the following line of JavaScript would change the value of the FirstName field to "Nate": document.forms[0].FirstName.value = "Nate"; The following if block would execute only if the first element of the CCType dropdown is currently selected, or if no selection has been made at all: if (document["SignupForm"].CCType.selectedIndex < 1) { alert("Don't leave the credit card type blank!"); } The following would get the value of the currently selected option in the drop-down list (. In an actual code file, this should be all on one line, but it's too long to print in this book): document["SignupForm"].CCType.options[ document["SignupForm"].CCType.selectedIndex].value You can use JavaScript's with statement to make that last snippet a little easier to type and read, like so: with (document["SignupForm"].CCType) { options[selectedIndex].value; } Note that if the <select> allows multiple selections via the multiple attribute, you must iterate through the element.options array, accessing each option's selected property to find out whether it is selected. The number of options is always available as element.options.length, so the following snippet would allow you to perform some action for each selected option: with (document["SignupForm"].CCType) { for (var i = 0; i < options.length; i++) { if ( options[i].selected ) { // ...your code here... } } } You will see further examples of working with form fields in nearly every example listing in this chapter. |