IntermediateLesson 13 of 307 min read

Textarea, select, checkboxes and radio buttons

Choose the right control for every question: text areas, drop-downs, radio buttons, checkboxes and suggestion lists.

In this lesson you will

  • Collect long answers with textarea
  • Offer choices with select, radio and checkbox
  • Group controls with fieldset
  • Pick the right control for each question

About 13 minutes

When one line is not enough

A restaurant’s booking page asks for a name, a party size, a seating area and any special requests. A text box fits the name. The party size is a choice from a short list. The seating area is one choice from three. The special requests might run to several sentences. Each of those needs a different control, and HTML has one for every case.

Textarea for longer writing

The <textarea> element is a multi-line box. Unlike input, it has a closing tag, and anything between the tags is the starting text.

<label for="msg">Special requests</label>
<textarea id="msg" name="requests" rows="5" cols="40" maxlength="500"></textarea>

rows sets how tall the box is, cols how wide, and maxlength caps the number of characters. In practice you will set the width in CSS and let people drag the corner to resize.

Select for a list of choices

A <select> shows a drop-down list. Each option is an <option>. The value is what is sent, while the text between the tags is what people read.

<label for="area">Seating area</label>
<select id="area" name="area">
  <option value="">Choose one</option>
  <option value="indoor">Indoors</option>
  <option value="terrace">Terrace</option>
  <option value="bar">At the bar</option>
</select>

Long lists become easier to scan when grouped with <optgroup label="...">. Add multiple to allow several choices, although on most sites checkboxes are friendlier than a multi-select, which many people do not know how to use.

Few options? Skip the drop-down

A drop-down hides its choices behind a click. If there are only two to five options, radio buttons show everything at once and save a tap.

Radio buttons: choose exactly one

Radio buttons are named after old car radios where pressing one button popped the others out. Give all buttons in a group the same name, and the browser lets only one be selected.

<fieldset>
  <legend>Seating area</legend>
  <label><input type="radio" name="area" value="indoor" checked> Indoors</label>
  <label><input type="radio" name="area" value="terrace"> Terrace</label>
  <label><input type="radio" name="area" value="bar"> At the bar</label>
</fieldset>

The shared name creates the group. The different value on each says which one was picked. The checked attribute preselects one. Wrapping the group in a fieldset with a legend gives the question itself a label.

Checkboxes: choose any number

Checkboxes are independent switches. Each can be on or off. Use them for yes or no questions and for choosing several items from a list.

<fieldset>
  <legend>Extras</legend>
  <label><input type="checkbox" name="extras" value="candles"> Birthday candles</label>
  <label><input type="checkbox" name="extras" value="card"> Greeting card</label>
  <label><input type="checkbox" name="extras" value="box" checked> Gift box</label>
</fieldset>
<label><input type="checkbox" name="news"> Send me offers by email</label>

An unchecked checkbox sends nothing at all, so the server only hears about the ones that are ticked. Never pre-tick the box for marketing emails: in many countries that breaks consent rules, and it annoys people.

Turn this plain text box into a proper question: replace it with three radio buttons for Small, Medium and Large. They must share one name, and Medium should be preselected.

<fieldset>
  <legend>Cake size</legend>
  <input type="text" name="size">
</fieldset>

Use three labels each containing an input of type radio with name=”size”. Add checked on the Medium one.

<fieldset>
  <legend>Cake size</legend>
  <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>
</fieldset>

Suggestions with datalist

Sometimes you want people to type freely but offer suggestions as they go. A <datalist> attached to an input through its list attribute does exactly that.

<label for="city">City</label>
<input type="text" id="city" name="city" list="cities">
<datalist id="cities">
  <option value="Lahore">
  <option value="Karachi">
  <option value="Islamabad">
</datalist>

Which control for which question?

  • A short answer, such as a name: input type="text".
  • A long answer, such as feedback: textarea.
  • One choice from many options: select.
  • One choice from a few options: radio buttons.
  • Several choices at once, or a yes/no: checkboxes.
  • A free answer with suggestions: input with datalist.

Complete this form: add a textarea named feedback with 4 rows, then a select named rating with options 1 to 5.

<form>
  <label for="fb">Your feedback</label>
  <!-- textarea here -->
  <label for="rt">Rating</label>
  <!-- select here -->
</form>

Give the textarea id=”fb” and the select id=”rt” so the labels connect.

<form>
  <label for="fb">Your feedback</label>
  <textarea id="fb" name="feedback" rows="4"></textarea>
  <label for="rt">Rating</label>
  <select id="rt" name="rating">
    <option>1</option><option>2</option><option>3</option><option>4</option><option>5</option>
  </select>
</form>
Common mistake

Giving each radio button a different name breaks the group, and people can tick them all. One question, one name.

Designing a form people finish

Knowing the controls is half the job. The other half is arranging them so that people actually reach the button. Researchers who study forms keep finding the same things. People abandon long forms, they trip over unclear questions, and they hate being asked for something twice. Here is how those findings turn into habits.

  • Put one question per row on small screens. Two fields side by side are easy to skip on a phone.
  • Put the label above the field, not beside it. It reads naturally from top to bottom and still fits when the screen is narrow.
  • Order questions as a conversation would. Name, then contact details, then the request. Do not jump back and forth.
  • Say why you ask. A short note such as “We only use your phone number to confirm the delivery” removes hesitation.
  • Make the button say what happens. “Reserve my table” beats “Submit” every time.

Try this on your own forms. Read each field out loud and ask whether a stranger would know exactly what to type. If you hesitate, they will too.

Good to know

Everything in this lesson works the same way inside WordPress form plugins. They simply generate this HTML for you. Knowing what the plugin produces helps you fix a form that behaves strangely.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What makes several radio buttons act as one group?

  1. The same id
  2. The same name
  3. The same value
  4. The same class

Radio buttons with an identical name allow only one selection.

Which control suits choosing several extras from a list?

  1. Radio buttons
  2. Checkboxes
  3. A single text box
  4. A range slider

Checkboxes are independent, so any number can be ticked.

What is sent for a select option?

  1. Always the visible text
  2. Its value attribute
  3. The option’s position number
  4. Nothing

The value is sent. The text is only for reading.

How does a textarea differ from an input?

  1. It has a closing tag and allows several lines
  2. It cannot have a name
  3. It only accepts numbers
  4. It is always required

textarea is a multi-line box with an opening and closing tag.

Which attribute preselects a radio button or checkbox?

  1. selected
  2. checked
  3. default
  4. on

checked preselects radios and checkboxes. selected is for option elements.

Summary

  • Textarea collects several lines; select offers a drop-down.
  • Radio buttons share a name and allow one choice.
  • Checkboxes are independent and allow several.
  • Datalist suggests values while people type.

Next lesson

Your form can now ask everything. Next you will make the browser check the answers before sending.

Questions

Radio or select?

For two to five options, radio buttons are quicker because everything is visible. For long lists such as countries, a select saves space.

Can I style a select box freely?

Only partly. Browsers draw the open list themselves. Many sites use a custom component, but the native select is the most accessible and works best on phones.

What does multiple do on a select?

It lets people choose more than one option by holding a key, but checkboxes are usually easier to understand.

Progress is saved in this browser.