HTML・Taking user input

Choosing inputs (checkboxes, radio buttons, select)

Inputs where you pick from ready-made options instead of typing. Checkboxes let you choose as many as you like, radio buttons only one, and a select gives you a dropdown — let's learn all three ways to let people choose.

So far, the input boxes have been about “typing text in.” But forms have another shape too: inputs where you pick from ready-made options. Think of a survey that says “Choose the ones that apply” — let’s learn how to build that.

Pick as many as you like: checkboxes

type="checkbox" is a square box that lets you pick as many of the matching options as you want.

<label><input type="checkbox"> Apple</label>
<label><input type="checkbox"> Orange</label>
<label><input type="checkbox"> Grape</label>

Wrapping it in a <label> makes it so that clicking the text also ticks the box (that’s the label effect you learned earlier).

Try clicking them yourself. Clicking the text part also ticks the box. You can pick as many as you like.

Pick just one: radio buttons

type="radio" is a round button for picking just one from a set of options. Use it for mutually exclusive choices, like “Yes / No.”

<label><input type="radio" name="plan"> Free plan</label>
<label><input type="radio" name="plan"> Paid plan</label>
When you pick one, the other turns off automatically — try it and see. That’s because they share the same name.

Choose from a dropdown: select

When there are lots of options, a <select> dropdown keeps things tidy. You line up <option> (the choices) inside it.

<select>
  <option>Tokyo</option>
  <option>Osaka</option>
  <option>Fukuoka</option>
</select>

The <select> is the box, and each <option> is a single choice. When you click it, the list pops open.

Try clicking it to open it. The list pops open and you can pick a choice.

Choosing something in advance (checked, selected)

You can also have something already selected the moment the page opens. For checkboxes and radio buttons, just add checked; for a select, add selected to the <option> you want chosen.

<label><input type="checkbox" checked> Apple</label>

<label><input type="radio" name="plan" checked> Free plan</label>
<label><input type="radio" name="plan"> Paid plan</label>

<select>
  <option>Tokyo</option>
  <option selected>Osaka</option>
  <option>Fukuoka</option>
</select>
What you writeEffect
checkedTurns a checkbox or radio button on from the start
selectedMakes that option chosen from the start

You use this for things like “pre-select the most commonly chosen item” or “set a default answer for a survey.”

The moment the page opens, Apple is ticked, Free plan is selected, and the select shows Osaka — it starts in this state without you clicking anything.

Give it a try

Here are all three kinds, laid out for real. Try selecting them yourself.

With the radio buttons, check that picking one automatically turns off the other. That’s because they share the same name.
HTMLBundle your inputs with a formThe next lesson covers how to bundle typing inputs and choosing inputs together and send them off.

Summary of this lesson

  1. Checkboxes (type="checkbox") let you pick several
  2. Radio buttons (type="radio") let you pick just one — give siblings the same name
  3. Select (<select> and <option>) lets you choose from a dropdown

Typing inputs and choosing inputs — you’ve got all the parts now.

Try it

The “checkbox, radio, select” you learned this time are not used on Shima’s page. They’re tools for surveys and sign-up forms. Keep them in mind and use them when you need them.

FAQ

Can I make a checkbox already checked from the start?
Yes. Just add checked to the input, and it will be ticked the moment the page opens.
What happens if I forget to give radio buttons a name?
They won't form a single group, and several buttons will be selectable at the same time. A radio button is a way to pick just one from a set of siblings that share the same name, so matching up the name is essential.
How do I change which item shows first in a select?
Add selected to the option you want to be chosen. If you don't write anything, the topmost option shows first.