Radio Buttons in HTML | Codabase

Radio Buttons in HTML

5 Min Read

Radio buttons are essential components of web forms, allowing users to select one option from a set of predefined choices. In this comprehensive guide, we’ll dive into the world of radio buttons in HTML, covering their creation, attributes, styling, and more.

Creating Radio Buttons with HTML

To create radio buttons in HTML, you’ll use the <input> element with the type attribute set to radio. The following example demonstrates how to create a simple set of radio buttons for a user to select their favorite color:

<form>
  <label><input type="radio" name="color" value="red"> Red</label>
  <label><input type="radio" name="color" value="green"> Green</label>
  <label><input type="radio" name="color" value="blue"> Blue</label>
</form>

Note that each radio button shares the same name attribute, which is essential for grouping them together. When the form is submitted, the value of the selected radio button will be sent to the server.

Common Radio Button Attributes

Beyond the type and name attributes, there are several other attributes that can enhance your radio buttons:

Value Attribute

The value attribute represents the data that will be sent to the server when the form is submitted. It’s essential to include a unique value for each radio button in a group.

Checked Attribute

The checked attribute sets the default selected radio button in a group. Only one radio button in a group should have this attribute:

<label><input type="radio" name="color" value="red" checked> Red</label>

Disabled Attribute

The disabled attribute disables a radio button, preventing users from interacting with it. This can be useful when you want to display an option that is not currently available:

<label><input type="radio" name="color" value="purple" disabled> Purple (Unavailable)</label>

Styling Radio Buttons with CSS

While the default appearance of radio buttons can vary between browsers, you can style them using CSS to create a consistent look. One popular technique is to hide the default radio button and create a custom one using the ::before and ::after pseudo-elements:

input[type="radio"] {
  position: absolute;
  opacity: 0;
}

input[type="radio"] + label::before {
  content: "";
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid #333;
  border-radius: 50%;
  margin-right: 5px;
}

input[type="radio"]:checked + label::after {
  content: "";
  display: inline-block;
width: 12px;
height: 12px;
background-color: #333;
border-radius: 50%;
position: relative;
top: 2px;
left: 2px;
}

Using this approach, you can customize the appearance of your radio buttons to match your website’s design. You can also add hover and focus states to improve the user experience:

input[type="radio"]:hover + label::before {
  border-color: #555;
}

input[type="radio"]:focus + label::before {
  outline: auto 5px -webkit-focus-ring-color;
}

Handling Radio Button Events with JavaScript

JavaScript can be used to add interactivity to your radio buttons. For instance, you can create an event listener that listens for the change event on each radio button, then performs an action based on the selected option:

const radioButtons = document.querySelectorAll('input[type="radio"]');

radioButtons.forEach((button) => {
  button.addEventListener('change', (event) => {
    console.log(`Selected color: ${event.target.value}`);
  });
});

Using this code, the selected color will be logged to the console each time the user changes their selection.

In conclusion, mastering radio buttons in HTML is crucial for creating user-friendly web forms. By understanding how to create, style, and interact with radio buttons, you’ll be able to provide a seamless and accessible experience for your users.

Mastering JavaScript: A Comprehensive Guide to DOM Manipulation and Event Handling for Interactive Websites
Powerful Python Tips: Web Scraping
The Ultimate Guide to API Integration: Connecting Your App with RESTful Services Using Postman and Swagger
Getting Started with Kubernetes: A Comprehensive Minikube Guide
Monitoring and Logging in Kubernetes: Best Practices and Tools

W3Schools – HTML Forms
MDN Web Docs –
CSS-Tricks – Custom Radio Buttons
W3C Web Accessibility Tutorials – Form Controls
MDN Web Docs – Element: change event

Share this Article
Leave a comment