Checkbox
A clicked renders a special type of button that can be seelcted to indicate a "true" value or deselected to indicate a "false" value.
Basics
Bare-bones code for a checkbox is:
<INPUT TYPE="checkbox">
This renders the checkbox only and not any associated label.
Usage
A single checkbox provides an independent true/false value, and is a natural for binary questions (yes or no). However, they are also commonly used in sets to enable the user to "check all that apply."
Processing
How a checkbox is passed depends on the browser.
All browsers will pass a name=value pair of some kind to the server when the checkbox is checked at the instant the form is submitted, but the value passed is not consistent. It should be the content of the VALUE attribute, but in some instances, it is "true" or "on" or "checked" - but fortunately, it's always non-null value.
If the checkbox is not checked, some browsers will pass a null, others won't pass a name=value pair at all.
HTML Attributes
The checkbox usually has only a name and value, but other attributes are available:
- CHECKED - Indicates the checkbox should render as checked.
- ACCESSKEY - A shortcut key to put focus on the element.
- DISABLED - Grays out the element and prevents the user from altering its value.
- TABINDEX - Specified as a number, this alters the default tabbing order of a form.
The VALUE for a checkbox is optional - browsers will pass a non-null value if it is not specified. Some browsers will pass a default value even if it is.
JavaScript
Properties
- checked - Indicates whether the box is currently checked.
- defaultChecked - Indicates whether the box is checked by default.
Methods
- click() - Simulates a user click on the checkbox in its current state.
Event Handlers
- onclick - Takes effect when the element is clicked
Misc Notes
If you need to provide a set of checkboxes and allow the user to check only two of them, a JavaScript hack will be needed.