The HTML form is where we place elements making up the interface in a page.
Each HTML form groups together a set of HTML elements. When a form is submitted to a server for processing, all the data in that form is sent to the server. We can have multiple forms on a page, but only the information in one form can be sent to the server.
A form is created using the opening tag <form> and ended with a close tag </form>. All the elements we want included in that form are placed in between the open and close <form> tags. The <form> tag has various attributes—for client-side scripting, the name attribute is the important one. We can access forms with either their name attribute or their ID attribute.
Each <form> element creates a Form object, which is contained within the document object. To access a form named myForm, we write document.myForm. The document object also has a forms property, which is an array containing every form inside the document. The first form in the page is document.forms, the second is document.forms, and so on. Using the length property of an Array object, document.forms.length tells us how many forms there are on the page.
We also saw that the methods focus() and blur(), and the events onfocus and onblur, are available to every form element object. Such an element is said to receive the focus when it becomes the active element in the form, either because the user has selected that element or because we used the focus() method. However an element got the focus, its onfocus event will fire. When another element is set as the currently active element the previous element is said to lose its focus, or to blur. Again, loss of focus can be the result of the user selecting another element or the use of the blur() method; either way when it happens the onblur event fires. We saw that onfocus and onblur can, if used carefully, be a good place to check things like the validity of data entered by a user into an element.
All elements return a value, which is the string data assigned to that element. The meaning of the value depends on the element; for a text box, it is the value inside the text box, and for a button, it's the text displayed on its face.
Having discussed the common features of elements, we then looked at each of the more commonly used elements in turn, starting with the button element.
The button element's purpose in life is to be clicked by the user, where that clicking fires some script we have written. We can capture the clicking by connecting to the button's onclick event. A button is created using the <input> tag with the type attribute set to button. The value attribute determines what text appears on the button's face. Two variations on a button are the submit and reset buttons. In addition to acting as buttons, they also provide a special service not linked to code. The submit button will automatically submit the form to the server. The reset button clears the form back to its default state when loaded in the page.
The text element allows the user to enter a single line of plain text. A text box is created using the <input> tag, with the type attribute set to text. We can set how many characters the user can enter and how wide the text box is with the maxlength and size attributes of the <input> tag. The text box has the associated object called Text, which has the additional events onselect and onchange. The onselect event fires when the user selects text in the box, and the more useful onchange event fires when the element loses focus and its contents have changed since the element gained the focus. The onchange event is a good place to do validation of what the user has just entered. If she entered illegal values, such as letters when we wanted numbers, we can let the user know and send her back to correct her mistake. A variation on the text box is the password box, which is almost identical to the text box except the values typed into it are hidden and shown as an asterisk. Additionally, the text box also has the onkeydown, onkeypress, and onkeyup events
The next element we looked at was the textarea, which is similar to the text box except it allows multiple lines of text to be entered. This element is created with the open tag <textarea> and closed with the </textarea> tag, the width and height in characters of the text box being determined by the COLS and ROWS attributes. Whether the textarea wraps text that reaches the end of a line and whether that wrapping is sent when the contents are posted to the server is determined by the wrap attribute. If left off, or set to off, no wrapping occurs; if set to soft, it causes wrapping client-side, but it's not sent to the server when the form is sent; if set to hard, it causes wrapping client-side and is sent to the server. The associated Textarea object has virtually the same properties, methods, and events as a Text object.
We then looked at the checkbox and radio button elements together. Essentially they are the same type of element except that the radio button is a grouped element. By this I mean that only one in a group can be checked at once. Checking another one causes the previously checked button to be unchecked. Both elements are created with the <input> tag, the type attribute being checkbox or radio. If checked is put inside the <input> tag, that element will be checked when the page is loaded. Creating radio buttons with the same name creates a radio button group. The name of a radio button actually refers to an array, and each element within that array is a radio button defined on the form to be within that group. These elements have associated objects called Checkbox and Radio. Using the checked property of these objects, we can find out whether a checkbox or radio button is currently checked. Both objects also have the onclick event in addition to the common events, onfocus and onblur.
Next in our look at elements were the drop-down list and list boxes, both in fact are actually the same select element with just the size attribute deciding whether it's a drop-down or list box. The <select> tag creates these elements, the size attribute determining how many list items are visible at once. If a size of 1 is given, a drop-down box rather than a list box is created. Each item in a select element is defined by the <option> tag, or added to later using the Select object's options array property, which is an array containing each Option object for that element. However, adding options after the page is loaded is different for Netscape and Microsoft browsers. The Select object's selectedIndex property tells us which option is selected; we can then use that value to access that option in the options array and use the Option object's value property. The Option object also has the text and index properties, text being the displayed text in the list and the index being its position in the Select object's options array property. We can loop through the options array, finding out its length from the Select object's length property. The Select object has the onchange event, which fires when the user selects another item from the list.
Finally we added a basic user interface to the Trivia Quiz. Now questions are created dynamically with the document.write() method and the user can select his answer from a group of radio buttons.