Using html5 checkbox 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:
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:
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.
html5 checkbox 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:
In the old days of XHTML, where each attribute should always have a value, even the boolean attributes, it would look like this:
Either way should work in all modern browsers, but the first way is shorter and more “HTML5-like”.
html5 checkbox Multiple choices
So far, all our checkboxes have been simple switches, e.g. for defining whether an option is on or off. html5 checkbox. 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:
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. html5 checkbox. 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:
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. html5 checkbox. 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
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. html5 checkbox. 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.
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. html5 checkbox. 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.