The community is working on translating this tutorial into Kiswahili, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".



Using checkboxes is a good option when you want to give your visitors the option to choose several items from a group of choices. In that regard, the checkbox works opposite of a radio button, which only allows you to select one item from a group of choices. In its most simple form, a checkbox is simply an input element with the type property set to checkbox, like this:

<input type="checkbox">

However, as with all input elements, you need to define a name for it to be usable - without a name, the element won't be identifiable when posting the form back to a server for processing. You also want to set a value - this will be the value sent to the server if the checkbox has been checked. Here's an example:

<input type="checkbox" name="nameOfChoice" value="1">

With this example, if the checkbox has been checked and the form is submitted to a server, the server will be able to read the form element "nameOfChoice" and its value will be 1.

Checked or not checked?

Notice how all the checkboxes so far have not been checked from the beginning - the user would have to interact with the checkbox to change its state from unchecked to checked. This might be what you want, but sometimes, you want the checkbox to be checked by default, either to suggest a choice to the user or because you are showing a checkbox with a value that corresponds to an existing setting, e.g. from a database. Fortunately, this is very simple - just add the checked attribute to the checkbox:

<input type="checkbox" name="nameOfChoice" value="1" checked>

In the old days of XHTML, where each attribute should always have a value, even the boolean attributes, it would look like this:

<input type="checkbox" name="nameOfChoice" value="1" checked="checked" />

Either way should work in all modern browsers, but the first way is shorter and more "HTML5-like".

Multiple choices

So far, all our checkboxes have been simple switches, e.g. for defining whether an option is on or off. Checkboxes are great for that, but as mentioned, they can also be used to allow the user a selection of possible options. Let me show you a neat example where this makes sense:

<form method="post" action="/Tests/Post/">      
<legend>What is Your Favorite Pet?</legend>      
<input type="checkbox" name="favorite_pet" value="Cats">Cats<br>      
<input type="checkbox" name="favorite_pet" value="Dogs">Dogs<br>      
<input type="checkbox" name="favorite_pet" value="Birds">Birds<br>      
<input type="submit" value="Submit now" />      

Notice how we now have multiple checkboxes, but they all share the same name ("favorite_pet") but different values (e.g. "Dogs"). When this form is submitted back to the server, all these checkboxes will be represented by a single name, but the value will be an array of 0-3 items. If you had used radio buttons instead of checkboxes, the user would only be allowed to pick a single favorite animal, but with checkboxes, they can select none of them, all of them or some of them.

Labels for checkboxes

If you tested the previous example, you will notice that we can put text next to a checkbox, but they are still two separate things - you can't click the text to trigger the checkbox. This can be really annoying for the user, but fortunately for us, it's easy to solve: Just use the label element! Here's a basic example to show you the difference:

<input type="checkbox" name="favorite_pet" value="Cats">Cats<br>  
<input type="checkbox" name="favorite_pet" value="Dogs" id="dogs">
<label for="dogs">Dogs</label><br>

Two checkboxes - one without a label and one with. They might look almost identical, but the one with the label can be triggered by clicking both the actual checkbox and the attached label. This is nice if you're sitting on a desktop PC with a mouse, but even better when you're using a touch device like a smartphone, where small checkboxes can be hard to hit with your finger.

The label is very simple - it uses the for attribute to attach itself to a form element with a matching id attribute (notice how I have "dogs" in both places).

Working dynamically with a checkbox

Just like any other DOM element, you are able to manipulate a checkbox using JavaScript. In that regard, it could be interesting to check whether a checkbox is checked or not and perhaps add some logic to control how many options a user can select. To show you how this can be done, I have extended a previous example (the "Favorite Pet" selector) to include some JavaScript magic:

<form method="post" action="/Tests/Post">  
<legend>What is Your Favorite Pet?</legend>  
<input type="checkbox" name="favorite_pet" value="Cats" onclick="return ValidatePetSelection();">Cats<br>  
<input type="checkbox" name="favorite_pet" value="Dogs" onclick="return ValidatePetSelection();">Dogs<br>  
<input type="checkbox" name="favorite_pet" value="Birds" onclick="return ValidatePetSelection();">Birds<br>  
<input type="submit" value="Submit now">  

<script type="text/javascript">  
function ValidatePetSelection()  
var checkboxes = document.getElementsByName("favorite_pet");  
var numberOfCheckedItems = 0;  
for(var i = 0; i < checkboxes.length; i++)  
if(numberOfCheckedItems > 2)  
alert("You can't select more than two favorite pets!");  
return false;  

Allow me to quickly run through what this code does. We have the same form as before, but we have added an event handler to each of the checkboxes, which causes them to call a JavaScript function (ValidatePetSelection) when the user clicks them. In this function, we get a hold of all the relevant checkboxes using the getElementsByName function and then we loop through them to see if they are checked or not - for each checked item, we add to a number. This number is then checked and if it exceeds two, then we alert the user about the problem (only two pets can be selected) and then we return false. Returning false will prevent the checkbox from being checked.

This was just a simple example of how to work with checkboxes using JavaScript. You can do much more, especially if you use a JavaScript framework like jQuery, which will make it a lot easier to select and manipulate DOM elements.


Checkboxes allow you to setup selectable options for your users - either to toggle a single setting on or off, or to allow for multiple choices, like in the Favorite Pet example. You should use labels to tie your checkbox and the descriptive text together, to allow the user to click a larger area when manipulating the checkbox - this is also good for assisting technologies like screen readers for visually impaired.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!