Definition and Usage
The type="radio" attribute of the <input> tag represents a radio button.
It is generally used as a group of radio buttons, and only one radio button can be selected from a given group.
To implement this, all grouped radio buttons within the <fieldset> tag must be assigned the same name attribute and value, while a different value is specified for each value attribute.
The name attribute specifies the name of the control. When the form is submitted, this name is used to identify the control.
Basic Example
<fieldset>
<legend>Select the web development languages you are interested in.</legend>
<div>
<input type="radio" id="html" name="interest" value="html">
<label for="html">HTML</label>
</div>
<div>
<input type="radio" id="css" name="interest" value="css">
<label for="css">CSS</label>
</div>
<div>
<input type="radio" id="js" name="interest" value="js">
<label for="js">JavaScript</label>
</div>
</fieldset>
The implicit role value of <input type="radio"> is radio.
Using Related Attributes
Let's learn about the attributes related to <input type="radio">.
checked Attribute
The checked attribute indicates whether the radio button is checked by default. Use the checked attribute to have the radio button start in a checked state when the page first loads. However, the checked attribute does not change when the user checks or unchecks the radio button after the page has loaded.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<input type="radio" checked>
disabled Attribute
Use the disabled attribute to disable the radio button.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<input type="radio" disabled>
value Attribute
value attribute sets the initial value.
This value is sent upon form submission only if the element is checked. If no value is specified, the default string on is submitted when the element is checked.
<fieldset>
<legend>Select the web development languages you are interested in.</legend>
<div>
<input type="radio" id="html" name="interest" value="html">
<label for="html">HTML</label>
</div>
<div>
<input type="radio" id="css" name="interest" value="css">
<label for="css">CSS</label>
</div>
<div>
<input type="radio" id="js" name="interest" value="js">
<label for="js">JavaScript</label>
</div>
</fieldset>
name Attribute
The name attribute specifies the name of the element.
When the name attribute is specified, its value is used as the name of the data sent when the form is submitted. In JavaScript, the value of the name attribute is also used as the name of the element in the form.elements API.
Grouping Multiple Radio Buttons
The name attribute can be used to group multiple <input type="radio"> elements together.
When multiple radio buttons share the same name value, the value of the selected radio button is submitted under that single name.
Grouping related radio buttons into a set makes it easier to identify which options within a specific group were selected, both from the markup structure and the submitted data.
<fieldset>
<legend>Select the web development languages you are interested in.</legend>
<div>
<input type="radio" id="html" name="interest" value="html">
<label for="html">HTML</label>
</div>
<div>
<input type="radio" id="css" name="interest" value="css">
<label for="css">CSS</label>
</div>
<div>
<input type="radio" id="js" name="interest" value="js">
<label for="js">JavaScript</label>
</div>
</fieldset>
In this case, even if a group contains multiple radio buttons, the user can select only one radio button.
required Attribute
The required attribute indicates that the radio button is a required control that the user must check before submitting the form. It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
To apply the required attribute to a radio button group sharing the same name, applying it to at least one radio button in the group makes the entire group a required field. However, for code readability, it is better to apply the required attribute to all radio buttons in the group.
Specifying It on at Least One Radio Button in the Group
Applying the required attribute to at least one radio button within the group makes the entire radio button group a required field.
<form>
<fieldset>
<legend>Color selection is required</legend>
<p>
<!-- Apply the required attribute to at least one radio button in the group -->
<input id="red" type="radio" name="color" value="red" required>
<label for="red">Red</label>
</p>
<p>
<input id="green" type="radio" name="color" value="green">
<label for="green">Green</label>
</p>
<p>
<input id="blue" type="radio" name="color" value="blue">
<label for="blue">Blue</label>
</p>
<p>
<button type="submit">Submit</button>
</p>
</fieldset>
</form>
You can see that the browser displays an error message for the required field.
Specifying It on All Radio Buttons in the Group
For code readability, it is better to apply the required attribute to all radio buttons in the group.
<form>
<fieldset>
<legend>Color selection is required</legend>
<p>
<input id="red" type="radio" name="color" value="red" required>
<label for="red">Red</label>
</p>
<p>
<input id="green" type="radio" name="color" value="green" required>
<label for="green">Green</label>
</p>
<p>
<input id="blue" type="radio" name="color" value="blue" required>
<label for="blue">Blue</label>
</p>
<p>
<button type="submit">Submit</button>
</p>
</fieldset>
</form>
You can see that the browser displays an error message for the required field.
Differences from Checkboxes
Checkboxes are similar to radio buttons, but there are some important differences.
Checkboxes are represented by <input type="checkbox">.
Differences in Checking and Unchecking a Single Control
| Radio Button | You cannot alternately check and uncheck a single radio button. |
|---|---|
| Checkbox | You can alternately check and uncheck a single checkbox. |
<input type="radio">
<input type="checkbox">
Differences in Grouping
When grouping radio buttons and checkboxes, they share a common requirement: grouped items must have the same name attribute value, while each item must have a distinct value attribute value.
However, grouped checkboxes allow the user to select multiple options, whereas grouped radio buttons allow only one option to be selected.
Grouped Checkboxes
<fieldset>
<legend>Select the web development languages you are interested in.</legend>
<div>
<input type="checkbox" id="html" name="interest" value="html">
<label for="html">HTML</label>
</div>
<div>
<input type="checkbox" id="css" name="interest" value="css">
<label for="css">CSS</label>
</div>
<div>
<input type="checkbox" id="js" name="interest" value="js">
<label for="js">JavaScript</label>
</div>
</fieldset>
Code Explanation
The <legend> element is used to provide a caption (a brief title or description) for the elements grouped by the parent <fieldset> element.
Grouped Radio Buttons
<fieldset>
<legend>Select the web development languages you are interested in.</legend>
<div>
<input type="radio" id="html" name="interest" value="html">
<label for="html">HTML</label>
</div>
<div>
<input type="radio" id="css" name="interest" value="css">
<label for="css">CSS</label>
</div>
<div>
<input type="radio" id="js" name="interest" value="js">
<label for="js">JavaScript</label>
</div>
</fieldset>
Accessibility Considerations
When using <input type="radio">, you should use it together with a <label> tag.
For more information, see Accessibility Considerations for the <input> Tag.
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<input type="radio"> |
1 | 12 | 1 | 1 |
checked
|
1 | 12 | 1 | 1 |
disabled
|
1 | 12 | 1 | 1 |
name
|
1 | 12 | 1 | 1 |
value
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
<input type="radio">
|
HTML Standard #radio-button-state-(type=radio) |