On this page13 sections
  1. Learning Objectives
  2. 1. <select> and <option>
  3. 2. <optgroup>: Grouped Options
  4. 3. <textarea>
  5. 4. <button>
  6. 5. <fieldset> and <legend>
  7. 6. <datalist>: Suggestions
  8. 7. <output>: A Live Result
  9. Quick Reference
  10. Common Beginner Mistakes
  11. Practice Questions
  12. Practice Answers
  13. Key Takeaways

Lecture 035: Form Elements

Category6: Forms
Lecture035 of 048
Estimated video length14 min
TargetHTML5 · any modern browser
LevelBeginner

Beyond <input>, a form can use drop-downs, grouped options, multi-line text, suggestion lists, framed groups of fields, and an area that shows a live result. This lecture goes through every form element HTML offers.


Learning Objectives

  • Use <select>, <option> and <optgroup>, including multiple choice
  • Use <textarea> for long text
  • Group fields with <fieldset> and <legend>
  • Offer suggestions with <datalist>
  • Show a calculated result with <output>

1. <select> and <option>

Example

html
<label for="city">City</label>
<select id="city" name="city">
  <option value="khi">Karachi</option>
  <option value="lhr" selected>Lahore</option>
  <option value="isb">Islamabad</option>
</select>
  • value is what gets sent; the text is what the visitor sees.
  • selected picks the starting option.

Show several rows, allow several choices

html
<label for="langs">Languages (hold Ctrl / ⌘ to pick several)</label><br>
<select id="langs" name="langs" size="4" multiple>
  <option>English</option>
  <option>Urdu</option>
  <option>Punjabi</option>
  <option>Pashto</option>
  <option>Sindhi</option>
</select>

2. <optgroup>: Grouped Options

Example

html
<select name="car">
  <optgroup label="Japanese">
    <option>Toyota</option>
    <option>Honda</option>
  </optgroup>
  <optgroup label="Korean">
    <option>Hyundai</option>
    <option>Kia</option>
  </optgroup>
</select>

3. <textarea>

Example

html
<label for="msg">Message</label><br>
<textarea id="msg" name="message" rows="4" cols="40" maxlength="200" placeholder="Up to 200 characters"></textarea>

Unlike <input>, it has a closing tag. Any starting text goes between the tags.


4. <button>

html
<button type="submit">Send</button>
<button type="reset">Clear</button>
<button type="button" onclick="alert('Hello!')">Say hello</button>

See Lecture 018 for button types.


5. <fieldset> and <legend>

A <fieldset> draws a frame around related fields; <legend> is its caption.

Example

html
<form>
  <fieldset>
    <legend>Delivery address</legend>
    <p><label>Street <input name="street"></label></p>
    <p><label>City <input name="city"></label></p>
  </fieldset>
  <fieldset disabled>
    <legend>Gift wrap (coming soon)</legend>
    <label><input type="checkbox" name="wrap"> Wrap it</label>
  </fieldset>
</form>

disabled on a fieldset disables every field inside it.


6. <datalist>: Suggestions

A text box that suggests values as you type, while still letting you type anything.

Example

html
<label for="browser">Your browser</label>
<input id="browser" name="browser" list="browsers">
<datalist id="browsers">
  <option value="Chrome">
  <option value="Firefox">
  <option value="Safari">
  <option value="Edge">
</datalist>

The input's list matches the datalist's id.


7. <output>: A Live Result

Example: move the slider and change the number:

html
<form oninput="total.value = Number(qty.value) * Number(price.value)">
  <input type="number" id="qty" value="2" min="1"> ×
  <input type="range" id="price" min="100" max="1000" step="50" value="300">
  = Rs <output name="total" for="qty price">600</output>
</form>

Quick Reference

ElementPurpose
<input>Single-line field (many types: Lecture 036)
<label>Caption for a field
<select> / <option> / <optgroup>Drop-down list
<textarea>Multi-line text
<button>A clickable button
<fieldset> / <legend>A group of fields with a caption
<datalist>Suggestions for an input
<output>The result of a calculation

Common Beginner Mistakes

1. <textarea />. It needs a real closing tag: <textarea></textarea>.

2. No value on options. The visible text is sent, which may not be what your server expects.

3. Mismatched list and id. The datalist then never appears.

4. A <select> with hundreds of options. Consider a <datalist> or a search box instead.


Practice Questions

  1. How do you make an option chosen by default?
  2. Which attribute lets visitors choose several options in a <select>?
  3. Which element groups options under a heading?
  4. Write a fieldset with the caption "Your details".
  5. How does an input connect to a <datalist>?
  6. What is <output> for?

Practice Answers

  1. Add selected to the <option>.
  2. multiple
  3. <optgroup label="…">
  4. <fieldset><legend>Your details</legend>…</fieldset>
  5. The input's list attribute equals the datalist's id.
  6. Showing the result of a calculation, such as a total.

Key Takeaways

  • <select> + <option> (+ <optgroup>) build drop-downs; multiple allows several.
  • <textarea> is for long text and has a closing tag.
  • <fieldset> + <legend> group related fields.
  • <datalist> suggests values; <output> shows results.