Open In App

HTML <input type=”radio”>

Last Updated : 23 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

HTML <input type=”radio”> creates a radio button input field allowing users to select one option from multiple choices. Only one radio button in a group can be selected at a time.

The <input type="radio"> element may have additional attributes like name to group-related radio buttons and value to assign a specific value to the selected option.

Note: The “value” attribute in radio buttons assigns a distinct identifier for each option. While not visible to users, this value is sent to the server upon submission, helping identify the selected radio button.

HTML <input type=”radio”> Syntax:

<input type="radio">

HTML <input type=”radio”> Example

Here is the basic implementation of HTML <input type=”radio”>.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>Document</title>
    <style>
      h2{
          color: green;
      }
    </style>
</head>
  
<body>
    <h2 >
      Welcome To GeeksforGeeks
      </h2>
    <p>Select a Technology Brand:</p>
  
    <div>
        <input type="radio" id="Netflix" 
               name="brand" value="Netflix">
        <label for="Netflix">Netflix</label>
    </div>
  
    <div>
        <input type="radio" id="Audi" 
               name="brand" value="Audi">
        <label for="Audi">Audi</label>
    </div>
  
    <div>
        <input type="radio" id="Microsoft" 
               name="brand" value="Microsoft" 
               checked>
        <label for="Microsoft">Microsoft</label>
    </div>
  
</body>
  
</html>


Output:

HTML <input type=”radio”>

Explanation

  • In the above example we includes a styled <h2> element with green color, welcoming users to GeeksforGeeks.
  • Radio Button Group contains a group of radio buttons for selecting a technology brand.
  • Three radio buttons are provided for Netflix, Audi, and Microsoft brands.
  • The radio button for Microsoft is preselected by default, indicating it as the initial choice.

HTML <input type=”radio”> Example: 

In this example we are creating three radio buttons labeled as Option 1, Option 2, and Option 3.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <style>
       
        h1 {
            color: green;
        }
  
        h3 {
            color: #333;
            margin-bottom: 20px;
        }
  
        input[type="radio"] {
            margin-right: 5px;
        }
    </style>
    <title>GeeksforGeeks</title>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2><input type="radio">
    </h2>
    <h3>Choose an Option:</h3>
  
    <label>
        <input type="radio" name="option" 
               value="option1"> Option 1
    </label>
  
    <label>
        <input type="radio" name="option" 
               value="option2"> Option 2
    </label>
  
    <label>
        <input type="radio" name="option" 
               value="option3"> Option 3
    </label>
</body>
  
</html>


Output:

HTML <input type=”radio”>

Explanation

  • In the above example we have three radio buttons labeled Option 1, Option 2, and Option 3.
  • Radio buttons allow users to select from different options.
  • Styling ensures clarity and readability of the content.

HTML <input type=”radio”> Use cases

1. How to add Radio Buttons in form using HTML ?

To add radio buttons in a form, use the <input type=”radio”> element within <form> tags, each with a unique name attribute.

2. How to get value of selected radio button using JavaScript ?

To get the value of the selected radio button using JavaScript, access the `value` property of the checked radio button element.

3. How to check whether a radio button is selected with JavaScript ?

To check if a radio button is selected with JavaScript, access the `checked` property of the radio button element and evaluate its value.

4. How to style label associated with selected radio input and checked checkboxes using CSS ?

To style the label associated with a selected radio input or checked checkbox using CSS, use the `:checked` pseudo-class to target the checked input and style its label accordingly.

5. How to Create Custom Radio Button using HTML and CSS ?

To create custom radio buttons using HTML and CSS, hide the default input, style a custom button, and use CSS to simulate checked states.

HTML <input type=”radio”> Supported Browsers: 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads