Menu
Coddy logo textTech

Radio Buttons

Part of the Fundamentals section of Coddy's HTML journey — lesson 41 of 60.

In HTML, radio buttons are used to create a set of mutually exclusive options, where the user can select only one option from the group. Radio buttons are created using the <input> tag with the type attribute set to "radio".

Here's the basic syntax for creating a radio button in HTML:

<input type="radio" id="option1" name="radiogroup" value="option1">
<label for="option1">Option 1</label>
  • <input>: The tag that defines the input field.
  • type="radio": An attribute that specifies the input type as a radio button.
  • id: An attribute that gives the radio button a unique identifier.
  • name: An attribute that groups radio buttons together. All radio buttons with the same name belong to the same group, and only one option within a group can be selected at a time.
  • value: An attribute that specifies the value associated with the radio button. This value is sent to the server when the form is submitted.
  • <label>: A tag that provides a clickable label for the radio button. The for attribute of the label should match the id attribute of the radio button.

Here's an example of how to create a group of radio buttons:

<form>
	<input type="radio" id="option1" name="mygroup" value="option1">
	<label for="option1">Option 1</label>
	<br>
	
	<input type="radio" id="option2" name="mygroup" value="option2">
	<label for="option2">Option 2</label>
	<br>
	
	<input type="radio" id="option3" name="mygroup" value="option3">
	<label for="option3">Option 3</label>
	<br>
</form>

In this example, we have a form with three radio buttons, all belonging to the same group because they share the same name attribute ("mygroup"). Each radio button has a unique id and value, and a corresponding label. When the user selects one of these options and submits the form, the value of the selected radio button will be sent to the server.

challenge icon

Challenge

Easy

Create an HTML document that includes a form with a group of radio buttons. The form should have the following elements:

  1. A group of three radio buttons with the name "color".
  2. The first radio button should have the id "red", the value "red", and a label with the text "Red".
  3. The second radio button should have the id "green", the value "green", and a label with the text "Green".
  4. The third radio button should have the id "blue", the value "blue", and a label with the text "Blue".
  5. Add a <br> tag after each label to create a line break.

Cheat sheet

Radio buttons create mutually exclusive options where only one can be selected from a group. Use <input type="radio"> with matching name attributes to group them together.

Basic radio button syntax:

<input type="radio" id="option1" name="radiogroup" value="option1">
<label for="option1">Option 1</label>

Key attributes:

  • type="radio": Specifies the input as a radio button
  • id: Unique identifier for the radio button
  • name: Groups radio buttons together (same name = same group)
  • value: Value sent when form is submitted
  • for: Links label to radio button using the id

Example of a radio button group:

<form>
	<input type="radio" id="option1" name="mygroup" value="option1">
	<label for="option1">Option 1</label>
	<br>
	
	<input type="radio" id="option2" name="mygroup" value="option2">
	<label for="option2">Option 2</label>
	<br>
	
	<input type="radio" id="option3" name="mygroup" value="option3">
	<label for="option3">Option 3</label>
</form>

Try it yourself

<!DOCTYPE html>
<html>
    <body>
        <!-- Write code here -->
    </body>
</html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals