checked (HTML attribute)
| Depr. | Version |
|---|---|
| No | HTML 2 |
| IE5.5+ | FF1+ | SA1.3+ | OP9.2+ |
|---|---|---|---|
| Full | Full | Full | Full |
Syntax
Description
In some circumstances, you may
need to present one or more checkboxes in such a way that the state of the
control at the point of page load is checked, and the user has to opt out
rather than opting in. Or perhaps you’re presenting the user with a
form—including a series of checkboxes—that he or she
has previously filled in, and you want to preserve the state of the
selections that were made. The checked attribute
allows you to set the checked state to “on” by default.
The
checked attribute is also used for radio
input controls. Unlike the checkbox, only one in a
range of related radio inputs can be selected (see the section on radio
inputs in type for more on this), and the
checked attribute is used to identify which one is
selected. If you accidentally mark as "checked" a
number of radio inputs that share a given name
attribute, the last one that’s marked as such will be selected.
Note
that the checkbox’s appearance differs between browsers and operating
systems—in some, the checked checkbox appears as a ticked or checked box,
while in others, it’s a crossed box. A radio input
that’s "checked" will almost certainly not
look like a tick or a check, but let’s not get hung up on appearances. The
key is to know that the right control is selected!
Example
This checked
attribute is applied to a newsletter signup checkbox:
<form>
<input type="checkbox" name="chknewsletter" id="chknewsletter"
checked="checked"/>
<label for="chknewsletter">Send me the monthly newsletter</label>
</form>
Value
"checked" is the
only possible value. If the input should be unchecked, simply omit the
attribute entirely.
Compatibility
| Internet Explorer | Firefox | Safari | Opera | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 5.5 | 6.0 | 7.0 | 1.0 | 1.5 | 2.0 | 1.3 | 2.0 | 3.0 | 9.2 | 9.5 |
| Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full |
It causes no compatibility issues, and has excellent support across all tested browsers.
User-contributed notes
There are no comments yet.
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.