Skip to main content

How to Make Radio Buttons in HTML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Radio buttons let people pick exactly one option from a short list. Give every <input type="radio"> in the list the same name and its own value, and the browser makes sure only one of them is selected at a time.

Syntax​

Syntax
<label><input type="radio" name="<group>" value="<value>"> <text></label>
<label><input type="radio" name="<group>" value="<value>" checked> <text></label>
PartWhat it does
type="radio"Makes the input a radio button.
name="<group>"The group's name. Radio buttons with the same name form one group, and only one in it can be selected. The form also sends the choice under this name.
value="<value>"What the form sends when this radio button is selected.
checkedOptional. Selects this radio button when the page opens.
<text>The words next to the radio button. Inside the same label as the input, clicking them selects it too.

MDN's input type="radio" page covers every attribute.

The Example​

The example is a small order form with three sizes, Small, Medium and Large, where Medium is selected from the start, and an Order button.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a form holding the text Choose a size:, three radio buttons named size with the values small, medium and large, each in a label, the medium one checked, and an Order button.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Radio buttons</title>
</head>
<body>
<form>
<p>Choose a size:</p>
<label><input type="radio" name="size" value="small"> Small</label>
<label><input type="radio" name="size" value="medium" checked> Medium</label>
<label><input type="radio" name="size" value="large"> Large</label>
<p><button type="submit">Order</button></p>
</form>
</body>
</html>

Step 2: Choose Large​

Double-click index.html to open it in your browser. Medium is selected, because it has checked. Click Large:

The page in Chromium: Choose a size: with the radio buttons Small, Medium and Large, Large selected, and an Order button.

Medium was unselected by itself when you chose Large. The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

Step 3: Send the Form​

Click Order. The page loads again, and the address now ends with index.html?size=large.

What Happened​

  • The three radio buttons are one group, because they share name="size", so selecting Large unselected Medium.
  • The form sent only the selected option: the group's name, size, with the selected button's value, large.
  • Clicking Large a second time didn't unselect it. A selected radio button stays selected until another one in its group is chosen.

Common Mistakes​

MistakeWhat happensDo this instead
Giving the radio buttons different namesThey're no longer one group: with Large renamed to name="sizes", Small and Large could both be selectedGive every radio button in the group the same name
Leaving out valueThe form sends on instead of the option: with no value on Large, the address ended with ?size=onGive each radio button its own value
Using radio buttons when people may choose several optionsOnly one can ever be selectedUse checkboxes

Troubleshooting​

SymptomLikely causeFix
Two radio buttons are selected at onceTheir names differUse the same name
The form sends onThe selected radio button has no valueAdd a value
Clicking the words doesn't select the optionThe words aren't in a label with the inputWrap the input and its words in one label

FAQ​

How do people clear their choice?​

They can't, by clicking it again. If people must be able to choose nothing, add an option for that, such as None.

Can people choose with the keyboard?​

Yes. With Medium selected and focused, pressing the Down arrow key selected Large.

How do I start with no option selected?​

Leave checked off every radio button in the group.

Conclusion​

Give the radio buttons in a group the same name and each its own value, mark one with checked if you want a default, and the browser does the rest: one choice at a time, sent as name=value.

Additional Resources​