input (type=checkbox) element

If you don't know what an element is or how you must use it, I recommend you read the "HTML tags and attributes" tutorial that you can find in the HTML tutorials section.

Description

The input element, having the "checkbox" value in its type attribute, represents a two-states control that allows users to mark it as selected or deselected. This control may be used to collect information that answers simple true/false questions like "do you want/accept/deny this?".

The state of a checkbox control is decided by the presence of the boolean attribute checked (if it's present, the checkbox is checked). But when the form is submitted, what's sent of this element to the processing agent isn't the checked state, but the name/value pair. In the submission, the state of the checkbox decides wether the control's value should be sent with the form. In other words, the control is submitted only if it's checked.

In contrast to radio buttons that conform groups where only one option can be checked at a time, checkbox controls are independent.

Examples

Our first example shows a basic interests list where the user can check any of the options he enjoys doing in his free time. Here we'll be using the value attribute, a very rare practice in real cases, thanks to the fact that a checkbox is only submitted when it's checked.

When working with checkboxes, the value attribute can be safely omitted thanks to the possibility of knowing the state of a checkbox by verifying its presence/absence among the submitted fields.

<form action="../../form-result.php" method="post" target="_blank">
  <p>
    Interests:<br>
    <input type="checkbox" name="cb-cars" value="likes"> Cars<br>
    <input type="checkbox" name="cb-sports" value="likes"> Sports<br>
    <input type="checkbox" name="cb-videogames" value="likes"> Videogames
  </p>
  <p><input type="submit" value="Send data"></p>
</form>

Interests:
Cars
Sports
Videogames

As you may note in the previous example, the text beside each checkbox doesn't respond to the clicks as anyone would expect. In the following example, we'll solving this issue by converting this plain text into a label for the checkbox.

To achieve this, we'll wrap both, the text and the corresponding input with a label element. This way, we'll associate the checkbox with everything else inside the label.

<form action="../../form-result.php" method="post" target="_blank">
  <p>
    Interests:<br>
    <label><input type="checkbox" name="cb-cars"> Cars</label><br>
    <label><input type="checkbox" name="cb-sports"> Sports</label><br>
    <label><input type="checkbox" name="cb-videogames"> Videogames</label>
  </p>
  <p><input type="submit" value="Send data"></p>
</form>

Interests:


In our third example, we're making use of the checked attribute, to indicate the browser it should mark the checkbox when the page loads and when the form is reset.

<form action="../../form-result.php" method="post" target="_blank">
  <p>
    Interests:<br>
    <label><input type="checkbox" name="cb-html5" checked> HTML5</label><br>
    <label><input type="checkbox" name="cb-css3" checked> CSS3</label><br>
    <label><input type="checkbox" name="cb-javascript"> Javascript</label>
  </p>
  <p>
    <input type="submit" value="Send data">
    <input type="reset" value="Reset form">
  </p>
</form>

Interests:


Lastly, we're giving a try to the required attribute. When this boolean attribute is present, the browser won't allow the submission of the form until the checkbox has been checked. This can be of use in conformance requirements.

Browser support for the attribute required is incomplete. Authors may have to rely on scripts to provide this functionality cross-browser.

<form action="../../form-result.php" method="post" target="_blank">
  <p>
    To enter the program you must accept our terms of service<br>
    <label><input type="checkbox" name="cb-termsofservice" required> I accept the terms of service</label><br>
  </p>
  <p><input type="submit" value="Send data"></p>
</form>

To enter the program you must accept our terms of service

Attributes

Specific attributes

autofocus

A boolean value instructing the browser to set the focus to this control when the document has finished loading or when the dialog where the control finds itself is shown. If the attribute has the value "autofocus" or the empty string (""), or if it's just present, the control should get the focus as soon as possible, after the page or dialog has been loaded.

Example

<p><input type="checkbox" name="agreed" autofocus></p>

checked

A boolean value indicating if the control should be checked by default, this is, when the page loads or when the form is reset. If the attribute takes the value "checked" or the empty string (""), or if it's just present, the control will be checked by default.

Example

<form action="../../form-result.php" method="post" target="_blank">
  <p>
    Interests:<br>
    <label><input type="checkbox" name="cb-maths" value="likes" checked> Maths</label><br>
    <label><input type="checkbox" name="cb-art" value="likes"> Art</label>
  </p>
  <p>
    <input type="submit" value="Send data">
    <input type="reset" value="Reset form">
  </p>
</form>

Interests:

disabled

A boolean value indicating wether the control is disabled or not. If the attribute takes the value "disabled" or the empty string (""), or if it's just present, the control will be disabled.

Disabled controls are rendered greyed out (if visible), are blocked from user interaction and, more importantly, their values (if any) aren't sent when the form is submitted.

Example

<form action="../../form-result.php" method="post" target="_blank">
  <p>
    <input type="checkbox" name="idontlike" disabled> I don't like this website
    <input type="submit" value="Send data">
  </p>
</form>

I don't like this website

form

The value of the id attribute of the form with which this control is associated to.

This attribute is new in HTML5 and helps defining the pertenence of controls in nested or distant forms.

Example

<p><input type="checkbox" name="ilikeit" form="form1"> I like it!</p>
<form id="form1" action="../../form-result.php" method="post" target="_blank">
  <p><input type="submit" value="Send data"></p>
</form>

I like it!

name

A name for the control. This name will be sent by the browser to the processing agent, paired with the content of the value attribute. Both attributes together will conform a name-value pair that will be used to process the form data.

Currently, the value isindex, formerly used in a special way by some browsers and included in the HTML standard, isn't permitted in this attribute.

Example

<form action="../../form-result.php" method="post" target="_blank">
  <p>
    <input type="checkbox" name="iamsure"> I am sure...
    <input type="submit" value="Send data">
  </p>
</form>

I am sure...

required

A boolean value indicating wether this control can be left dechecked. If this attribute has the value "required" or the empty string (""), or if it's just present, the user will have to check the control in order to be able to submit the form.

If a checkbox with the required attribute present is left dechecked, supporting browsers will throw an error upon submission and cancel the process immediately.

Browser support for the attribute required is incomplete. Authors may have to rely on scripts to provide this functionality cross-browser.

Example

<form action="../../form-result.php" method="post" target="_blank">
  <p>
    <input type="checkbox" name="acceptedterms" required> I accept your terms
    <input type="submit" value="Send data">
  </p>
</form>

I accept your terms

type

A value indicating the type of the field that this element represents. There are twenty two possible values (case-insensitive):

  • hidden: a hidden control used to send information to the server, typically managed by scripts.
  • text: a control used to input a single-line piece of text.
  • search: same as text but for search purposes.
  • tel: a control used to provide a telephone number.
  • url: a text box used to input a single and absolute URL.
  • email: a control designed to edit one or more e-mail addresses.
  • password: a text box for editing passwords, where the characters are represented by dots.
  • date: a control to input a specific date.
  • month: a control to input a specific month.
  • week: a control to input a specific week.
  • time: a control to input a specific time.
  • datetime-local: a control to input a specific local date and time.
  • number: a control to input a number.
  • range: a control to input one or two numbers inside a range.
  • color: a control to input a color.
  • checkbox: a control to input a boolean value (true/false).
  • radio: a control used to choose one single option among many.
  • file: a control used to upload files to the server.
  • submit: a button used to submit the form.
  • image: same as submit but with the ability to be shown as an image instead of using the default button appearance.
  • reset: a button used to reset the form's controls to their default values.
  • button: a button with no default action associated.

When this attribute isn't present, the element behaves as it would have the value "text".

Example

<input type="checkbox" name="iagree">

value

A value for the control, that will be submitted to the processing agent, paired with the value of the name attributo, only if the checkbox has been checked.

In contrast with other controls, in checkboxes this attribute can be omitted. In such case, the browser will fill the attribute with a default value (commonly "on"). Being a checkbox sent only when it's checked, processing agents may deduct the state of the checkbox from its presence or absence in the submitted data.

Example

<form action="../../form-result.php" method="post" target="_blank">
  <p>
    <input type="checkbox" name="publicprofile" value="yes"> Make my profile public
    <input type="submit" value="Send data">
  </p>
</form>

Make my profile public

Global attributes

For information about global attributes refer to this list of global attributes in HTML5.

Events

Global events

For information about global events refer to this list of global events in HTML5.