Programming Label and Fieldset Elements
Labels are used to associate HTML contents with an Input element, and
fieldsets are used to group multiple controls. The Label and Fieldset elements
are currently supported only by Internet Explorer 4.0.
The Fieldset element is useful for grouping different input controls
within a single formfor example, to group the shipping address and the
receiving address on a single form. The Fieldset element does not expose anything
extra to the object model beyond the standard events and its attributes.
However, with event bubbling Fieldset elements can be used to provide custom
behavior to groups of controls.
Label elements are especially useful with check boxes and radio
buttons. Before Label elements, when a radio button or check box was used, it had
to be clicked on directly. Now a label associated with a button can also be
used to select and deselect the button. The advantage to using Label elements is
that they also provide a focus rectangle for the controls, making it clear what
the purpose and contents of the control are and providing an extra click
region that can be used to select the control. This feature can be added risk-free
to any Web page, as down-level browsers ignore the Label element.
The Label Element and onclick Events
The Label element has an interesting effect on the event model. The
default action of clicking on a Label element is that the associated control receives
the focus. Therefore, when the user clicks on a Label element, the Label and
all its parent elements receive the onclick event. If the default action is not
overridden, the referenced control is given the focus. If the referenced control
is a check box or radio button, the onclick event is then bubbled again from
that control. This second bubbling is what allows clicking on the label for a
radio button or check box to change the value.
Unless you need to distinguish between the user clicking on the label
and the user clicking on the control itself, attach your event handler to the
control, not the label. [Содержание]
|