Computer >> Computer tutorials >  >> Programming >> HTML

How do we use radio buttons in HTML forms?


Using HTML forms, you can easily take user input. The <form> tag is used to get user input, by adding the form elements. Different types of form elements include text input, radio button input, submit button, etc.

Let’s learn how to use radio buttons in HTML forms to get user input. Radio buttons are used when out of many options; just one option is required to be selected. They are also created using HTML <input> tag and the type attribute is set to radio.

Here are the attributes for radio button −

Sr.No
Attribute & Description
1
type
Indicates the type of input control and for checkbox input control it will be set to radio.
2
name
Used to give a name to the control which is sent to the server to be recognized and get the value.
3
value
The value that will be used if the radio box is selected.
4checked
Set to checked if you want to select it by default.                                             


How do we use radio buttons in HTML forms?

Example

 You can try to run the following code to learn how to work with radio buttons in HTML −

<!DOCTYPE html>
<html>
   <body>
      <head>
         <title>HTML Radio Button</title>
      </head>
      <p>Gender</p>
      <form>
         <input type="radio" name="gender" value="male">Male
         <br>
         <input type="radio" name="gender" value="female">Female
      </form>
   </body>
</html>