Loading...

HTML essentialsContent

Select, datalist, textarea & fieldset

5 min lesson

Next
  • HTML offers more than text inputs: dropdowns, suggestion lists, multi-line text and grouped fields.
  • <select> with <option> creates a dropdown menu for choosing one or more values.
  • <datalist> suggests values as the user types but still allows free text.
  • <textarea> is a multi-line text box for messages and comments.
  • <fieldset> groups related controls, and <legend> titles the group.
  • This builds on Forms & inputs: the same label, id and name rules apply.
  • The example shows a select, a datalist, a textarea and a fieldset with radio buttons; you will see all four working.
  • Next, Form validation & types focuses on checking what users type.
Keep in mind
  • Use select when only listed values are allowed; use datalist when suggestions are enough.
  • Use textarea for anything longer than one line.
  • Wrap related choices in fieldset with a legend.
  • Every control still needs a name and a label.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Form controls</title>
</head>
<body>
  <form action="/submit" method="post">
    <!-- select: the user must pick one option -->
    <label for="size">Size</label>
    <select id="size" name="size">
      <option value="s">Small</option>
      <option value="m">Medium</option>
      <option value="l">Large</option>
    </select>
    <br>
    <!-- datalist: suggestions, but the user may type anything -->
    <label for="city">City</label>
    <input id="city" name="city" list="cities">
    <datalist id="cities">
      <option value="Delhi">
      <option value="Mumbai">
      <option value="Chennai">
    </datalist>
    <br>
    <!-- textarea: multi-line message -->
    <label for="msg">Message</label>
    <textarea id="msg" name="msg" rows="3" cols="30"></textarea>
    <br>
    <!-- fieldset groups related radio buttons -->
    <fieldset>
      <legend>Contact me by</legend>
      <input type="radio" id="email" name="contact" value="email">
      <label for="email">Email</label>
      <input type="radio" id="phone" name="contact" value="phone">
      <label for="phone">Phone</label>
    </fieldset>
    <button type="submit">Send</button>
  </form>
</body>
</html>

What happens: A form with a "Size" dropdown (Small, Medium, Large), a "City" field with suggestions (Delhi, Mumbai, Chennai), a "Message" text area, a "Contact me by" group with Email and Phone radio buttons, and a "Send" button.

Breaking it down

select

<select id="size" name="size">

A dropdown menu. The name is the key sent to the server, and each option's value is what is sent.

datalist

<input list="cities"> ... <datalist id="cities">

Offers suggestions while typing but still allows free text. Connect it to the input via list and id.

textarea

<textarea rows="3" cols="30"></textarea>

A multi-line text box. Text goes between the tags, and rows/cols set the visible size.

fieldset

<fieldset><legend>...</legend>...</fieldset>

Draws a border around related controls. The legend gives the group a title.

radio group

<input type="radio" name="contact">

Radio buttons with the same name form a group where only one can be chosen.

Form controls

ControlBest for
selectChoosing from fixed options
datalistSuggestions with free text
textareaMulti-line messages
fieldsetGrouping related fields
legendTitling a fieldset group

A helpful analogy

These controls are like different blanks on a paper form: a dropdown is a box with fixed choices, a datalist is a blank with hints, a textarea is a wide lined space, and a fieldset is a drawn border around related questions.

Nesting rules

  • 1.option elements must be inside select or datalist, never loose.
  • 2.legend must be the first child of fieldset.

Common mistakes

Using different name values for radio buttons in one group

Avoid

<input type="radio" name="a"> <input type="radio" name="b">

Use instead

<input type="radio" name="contact"> <input type="radio" name="contact">

Only radio buttons with the same name form a group. Different names let the user pick both.

Confusing select and datalist

Avoid

<select> when you want free text plus hints

Use instead

<input list="cities"> with a datalist

select only allows listed values. datalist allows typing anything while suggesting common choices.

Quick tips

  • •Give textarea a sensible rows value so users see two or three lines without scrolling.
  • •Use fieldset around radio groups; screen readers announce the legend before each option.
Previous
Next