Lesson

HTML Form Elements

10 min reading
Free Course

HTML Form Elements: Dropdowns, Textareas, Fieldsets, and Datalists

While input handles single-line fields, HTML provides specialized form elements for multi-line messages, dropdown menus, grouped sections, and autocomplete suggestions. Choosing native form controls gives you keyboard navigation and screen reader support without extra JavaScript libraries.

Specialized Form Controls

  • textarea: Collects multi-line text such as comments or bios. Control height with rows and enforce character limits with maxlength.
  • select, option, and optgroup: Creates a dropdown menu. Wrap related options inside optgroup label="..." for clearer long lists.
  • fieldset and legend: Visually and semantically group related fields—like shipping address fields or radio button sets—with a shared caption.
  • datalist: Attaches a native autocomplete suggestion dropdown to a text input via matching list and id values while still letting users type custom entries.
  • output: Displays the calculated result of a form formula or slider control.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML Form Elements Demo</title>
    <style>
        form { max-width: 460px; font-family: system-ui, sans-serif; display: grid; gap: 14px; }
        fieldset { border: 1px solid #cbd5e1; border-radius: 8px; padding: 14px; display: grid; gap: 10px; }
        select, textarea, input { width: 100%; padding: 8px; box-sizing: border-box; }
    </style>
</head>
<body>
    <form>
        <fieldset>
            <legend>Project Preferences</legend>
            <label for="framework">Primary Stack</label>
            <select id="framework" name="framework">
                <optgroup label="Frontend">
                    <option value="react">React</option>
                    <option value="vue">Vue</option>
                </optgroup>
                <optgroup label="Backend">
                    <option value="laravel">Laravel</option>
                </optgroup>
            </select>

            <label for="city">City (Autocomplete)</label>
            <input id="city" name="city" list="city-options" placeholder="Start typing...">
            <datalist id="city-options">
                <option value="Dhaka"></option>
                <option value="London"></option>
                <option value="Toronto"></option>
            </datalist>
        </fieldset>

        <label for="notes">Project Notes</label>
        <textarea id="notes" name="notes" rows="4" placeholder="Describe your requirements"></textarea>
    </form>
</body>
</html>

Common Mistakes to Avoid

  • Putting default text inside textarea via a value attribute: Unlike input, textarea places its initial text directly between its opening and closing tags.
  • Omitting legend inside fieldset: Screen readers announce the legend as context for every field inside the group, so always make legend the first child of fieldset.
  • Expecting datalist to reject unlisted values: datalist only suggests options; use select when users must pick strictly from fixed choices.

Frequently Asked Questions

How do I allow multiple selections in an HTML select box?

Add the multiple boolean attribute on the select opening tag so visitors can pick more than one option.

Why use fieldset around radio buttons?

Wrapping radio buttons in fieldset with a legend ensures screen readers read the shared question before each individual radio option.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
HTML Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum