The HTML <input>
element with type="radio"
is a fundamental building block for creating forms where users can select one option from a predefined group. It’s widely used in surveys, registration forms, and interactive web applications. In this blog post, we’ll explore the functionality, syntax, and best practices for using the HTML radio button effectively.
What is the <input type="radio">
Tag?
The <input type="radio">
tag is used to create radio buttons, which are form elements allowing users to select a single option from multiple choices. These buttons are grouped using the name
attribute, ensuring that only one button within the group can be selected at a time.
Basic Syntax
Here’s the basic structure of a radio button:
<form>
<input type="radio" name="group1" value="option1"> Option 1<br>
<input type="radio" name="group1" value="option2"> Option 2<br>
<input type="radio" name="group1" value="option3"> Option 3<br>
</form>
Key Attributes:
type="radio"
: Specifies that the input is a radio button.name
: Groups radio buttons together. Buttons with the samename
belong to the same group.value
: Specifies the value submitted when the radio button is selected.checked
: Pre-selects a button when the form is loaded.
Practical Example
Below is a practical example of a form with a question and radio buttons for the answers:
<form>
<label>What is your favorite color?</label><br>
<input type="radio" id="red" name="color" value="red">
<label for="red">Red</label><br>
<input type="radio" id="blue" name="color" value="blue">
<label for="blue">Blue</label><br>
<input type="radio" id="green" name="color" value="green" checked>
<label for="green">Green</label><br>
<button type="submit">Submit</button>
</form>
Advanced Usage
Disabling a Radio Button
You can disable a radio button using the disabled
attribute:
<input type="radio" name="status" value="active"> Active<br>
<input type="radio" name="status" value="inactive" disabled> Inactive<br>
Using Radio Buttons with JavaScript
You can dynamically interact with radio buttons using JavaScript. For instance:
<script>
function showSelected() {
const selected = document.querySelector('input[name="color"]:checked').value;
alert("You selected: " + selected);
}
</script>
<form>
<input type="radio" name="color" value="red"> Red<br>
<input type="radio" name="color" value="blue"> Blue<br>
<input type="radio" name="color" value="green"> Green<br>
<button type="button" onclick="showSelected()">Show Selection</button>
</form>
Best Practices
- Use Labels: Always associate radio buttons with labels using the
for
attribute for better accessibility. - Group with
name
: Ensure all buttons in a group share the samename
to function properly. - Default Selection: Use the
checked
attribute to set a default option, improving the user experience. - Styling: Enhance the appearance of radio buttons using CSS for a more modern look.
Conclusion
The HTML radio button is an essential tool for creating interactive and user-friendly forms. By mastering its syntax and leveraging attributes like name
, value
, and checked
, you can build dynamic forms with ease. Whether you’re creating a survey or a multi-step form, radio buttons are a reliable choice for single-option selections.