Radio Buttons and Checkboxes
When the user should choose one of a few options, we use radio buttons: inputs with type="radio". Radio buttons work as a group: of all the radio buttons with the same name, only one can be chosen. When the user chooses another one, the previous choice disappears. Each radio button has a value attribute, which is sent when that button is chosen, for example size=large.
A checkbox, an input with type="checkbox", works on its own: the user can tick or untick each checkbox independently of the others. It's used for yes/no questions, such as agreeing to the terms. A ticked checkbox is sent, for example, as cheese=on; an unticked one isn't sent at all.
Radio buttons and checkboxes need labels too. We usually write the label after the input, so that the text is to the right of the button. Clicking the label also chooses the button, which makes the small button easier to hit.
This is how part of a pizza order could look:
<div>
<input type="radio" name="size" value="small" id="size-small">
<label for="size-small">Small</label>
<input type="radio" name="size" value="large" id="size-large">
<label for="size-large">Large</label>
</div>
<div>
<input type="checkbox" name="cheese" id="input-cheese">
<label for="input-cheese">Extra cheese</label>
</div>This code is displayed like this (try clicking it):
Instructions
- 1
In the first empty
div, create a radio button with the attributenamewith the valuelevel, the attributevaluewith the valuebeginnerand the attributeidwith the valuelevel-beginner. After it, create alabelwith the contentBeginnerthat belongs to this radio button. - 2
Below it, in the same
div, create a second radio button with the same namelevel, the valueadvancedand the idlevel-advanced, and after it a label with the contentAdvanced. Then run the code and try choosing one option and then the other. - 3
In the second empty
div, create a checkbox with the attributenamewith the valuenewsand the attributeidwith the valueinput-news. After it, create a label with the contentSend me news.