Java Reference
In-Depth Information
It's here that you loop through each element on the form using document.form1[controlIndex],
which returns a reference to the element object stored at the controlIndex index position.
You'll see that in this example the
element
variable is set to reference the object stored in the
form1
collection at the index position stored in variable
controlIndex
. Again, this is for convenient short-
hand purposes; now to use that particular object's properties or methods, you just type
element
, a
period, and then the method or property name, making your code easier to read and debug, which also
saves on typing.
You only want to see which check boxes have been checked, so you use the type property, which every
HTML form element object has, to see what element type you are dealing with. If the type is checkbox,
you go ahead and see if it's a checked check box. If so, you append its value to the message string in
compSpec. If it is not a check box, it can be safely ignored.
Finally, you use the
alert()
method to display the contents of your message string.
Selection Boxes
Although they look quite different, the drop-down list and the list boxes are actually both elements cre-
ated with the
<select>
tag, and strictly speaking they are both select elements. The select element has
one or more options in a list that you can select from; each of these options is defi ned by means of one or
more
<option/>
elements inside the opening and closing
<select>
tags.
The
size
attribute of the
<select/>
element is used to specify how many of the options are visible to
the user.
For example, to create a list box fi ve rows deep and populate it with seven options, your HTML would
look like this:
<select name=”theDay” size=”5”>
<option value=”0” selected=”selected”>Monday</option>
<option value=”1”>Tuesday</option>
<option value=”2”>Wednesday</option>
<option value=”3”>Thursday</option>
<option value=”4”>Friday</option>
<option value=”5”>Saturday</option>
<option value=”6”>Sunday</option>
</select>
Notice that the
<option/>
element for Monday also contains the attribute
selected
; this will make
this option selected by default when the page is loaded. The values of the options have been defi ned as
numbers, but text would be equally valid.
If you want this to be a drop-down list, you just need to change the
size
attribute in the
<select/>
element to
1
, and presto, it's a drop-down list.
If you want to let the user choose more than one item from a list at once, you simply need to add the
multiple
attribute to the
<select/>
defi nition.
The
<select/>
element creates a
Select
object. This object has an
options
collection property, which
is made up of
Option
objects, one for each
<option/>
element inside the
<select/>
element associated