TExcelTAD
Home › HTML › HTML Form Elements

HTML Form Elements

<input> is not the only control you can put in a form. HTML has a whole family of form elements for long text, drop-down lists, buttons, grouping and showing results. This lesson tours them all.

Reference

Element Purpose
<input> A single field; behaviour depends on type
<label> A caption for a control
<select> A drop-down (or scrolling) list
<option> One choice inside <select> or <datalist>
<optgroup> A labelled group of options
<textarea> A multi-line text box
<button> A clickable button
<fieldset> Groups related controls with a border
<legend> A caption for a <fieldset>
<datalist> Suggestions for an <input>
<output> Displays the result of a calculation

select, option and optgroup

<select> creates a drop-down list. Each <option> has a value (sent to the server) and visible text (shown to the user). Add selected to preselect one, multiple to allow several choices, and size to show several rows at once.

html · try it yourself
<form action="#">
  <label for="city">City:</label>
  <select id="city" name="city">
    <optgroup label="North">
      <option value="hn">Hà Nội</option>
      <option value="hp">Hải Phòng</option>
    </optgroup>
    <optgroup label="South">
      <option value="hcm" selected>Hồ Chí Minh City</option>
      <option value="ct">Cần Thơ</option>
    </optgroup>
  </select>

  <p><label for="langs">Languages (hold Ctrl/Cmd for several):</label><br>
  <select id="langs" name="langs" multiple size="4">
    <option>HTML</option>
    <option>CSS</option>
    <option>JavaScript</option>
    <option>Python</option>
  </select></p>
</form>

textarea

<textarea> holds multi-line text. Unlike <input>, its default content goes between the tags, and rows and cols set its starting size (CSS width/height work too).

html · try it yourself
<form action="#">
  <label for="msg">Your message:</label><br>
  <textarea id="msg" name="message" rows="5" cols="40">Hi there!
This text is on a second line.</textarea>
</form>

Tip: Whitespace inside <textarea> is kept exactly. If you indent the closing tag, those spaces become part of the value.

button

<button> can contain HTML (icons, bold text), unlike <input type="submit">. Its type is important:

type Behaviour
submit Submits the form — this is the default inside a form
reset Restores all controls to their initial values
button Does nothing by itself; use it with JavaScript

Forgetting type="button" on a helper button is a classic bug: clicking it unexpectedly submits the form.

fieldset and legend

<fieldset> draws a box around related controls and <legend> gives that box a title. Screen readers announce the legend, which makes groups of radio buttons much clearer. Adding disabled to a fieldset disables every control inside it.

html · try it yourself
<form action="#">
  <fieldset>
    <legend>Shipping</legend>
    <label><input type="radio" name="ship" value="std" checked> Standard (3–5 days)</label><br>
    <label><input type="radio" name="ship" value="exp"> Express (1 day)</label>
  </fieldset>
  <fieldset disabled>
    <legend>Gift options (coming soon)</legend>
    <label><input type="checkbox" name="wrap"> Gift wrap</label>
    <textarea name="card" placeholder="Card message"></textarea>
  </fieldset>
  <p>
    <button type="submit">Order</button>
    <button type="reset">Reset</button>
    <button type="button" onclick="alert('Just a normal button!')">Help</button>
  </p>
</form>

datalist

<datalist> offers suggestions while the user types, but still allows any value. Connect it with the input's list attribute, which must match the datalist's id.

output

<output> shows the result of a calculation. Its for attribute lists the ids of the controls the result depends on. Together with the form's oninput event, you can build a live calculator with almost no code.

html · try it yourself
<form onsubmit="return false" oninput="total.value = (Number(price.value) * Number(qty.value)).toLocaleString('en-US')">
  <label>Browser:
    <input list="browsers" name="browser" placeholder="start typing…">
  </label>
  <datalist id="browsers">
    <option value="Firefox"><option value="Chrome">
    <option value="Safari"><option value="Edge">
  </datalist>
  <p>
    <input type="number" id="price" name="price" value="45000" step="1000"> ₫ ×
    <input type="number" id="qty" name="qty" value="3" min="1" style="width:4em"> =
    <output name="total" for="price qty">135,000</output> ₫
  </p>
</form>

Change the price or quantity and the total updates instantly.

Note: All these controls take part in form submission only if they have a name and are not disabled.

Test yourself

What is the default type of a <button> inside a form?

  1. button
  2. reset
  3. submit
  4. none

Which element gives a caption to a <fieldset>?

  1. <caption>
  2. <legend>
  3. <label>
  4. <title>

How do you link an <input> to a <datalist id="colors">?

  1. <input list="colors">
  2. <input data="colors">
  3. <input for="colors">
  4. <input options="colors">