On this page13 sections
Lecture 035: Form Elements
| Category | 6: Forms |
| Lecture | 035 of 048 |
| Estimated video length | 14 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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
<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>valueis what gets sent; the text is what the visitor sees.selectedpicks the starting option.
Show several rows, allow several choices
<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
<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
<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>
<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
<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
<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:
<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
| Element | Purpose |
|---|---|
<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
- How do you make an option chosen by default?
- Which attribute lets visitors choose several options in a
<select>? - Which element groups options under a heading?
- Write a fieldset with the caption "Your details".
- How does an input connect to a
<datalist>? - What is
<output>for?
Practice Answers
- Add
selectedto the<option>. multiple<optgroup label="…"><fieldset><legend>Your details</legend>…</fieldset>- The input's
listattribute equals the datalist'sid. - Showing the result of a calculation, such as a total.
Key Takeaways
<select>+<option>(+<optgroup>) build drop-downs;multipleallows several.<textarea>is for long text and has a closing tag.<fieldset>+<legend>group related fields.<datalist>suggests values;<output>shows results.