webdevpuneet.com

Cascading Select — Dependent Dropdowns in HTML, CSS & JS

Cascading Select · Forms · Plain HTML, CSS & JS · Live preview

CategoryForms

About this UI Snippet

Cascading Select — Dependent Dropdowns with Progress Steps, Loading State and Safe Reset

Screenshot of the Cascading Select snippet rendered live

Cascading (dependent) selects appear wherever data is hierarchical: country then region then city, category then subcategory, make then model. Each choice fills the next list, and the important rule is that changing a parent must clear everything below it so nobody can submit an impossible combination. This version uses native <select> elements in plain HTML, CSS and vanilla JavaScript, and adds the parts a real form needs: a three-step progress indicator, a loading state for each level, protection against slow responses arriving late, a result card and a Continue button that stays disabled until the path is complete.

Native selects, custom-looking

The controls are real <select> elements, so they stay keyboard-accessible, screen-reader friendly and use the OS picker on mobile. The default arrow is removed with appearance: none and replaced by an SVG caret that swaps for a small spinner while a level loads. Each select has a name and required attribute, so new FormData(form) returns { country, state, city } directly on submit.

One reset rule, one load function

reset(from) clears a level and everything below it, restores the "Select a country first" placeholders, disables the selects and removes their hints. load(level) fetches the options for one level from the values above it and fills them in. Every change handler does the same two things in the same order: reset the levels below, then load the next one. That order is what keeps stale choices from surviving a parent change.

Loading and stale responses

getOptions() returns a promise after a short delay to imitate an API call, so you can see the spinner and replace the timeout with fetch(). Each level has a token that increases on every reset; when a response arrives, it is dropped if its token is no longer current. Without this guard, picking India and quickly switching to Australia could fill the region list with India's regions.

Progress steps and hints

A three-step indicator above the form marks the current step, turns finished steps into ticks and fills the connecting line as you move forward. Each label shows how many options are available ("3 regions"), and the select that just opened gets a soft pulse and keyboard focus so the flow continues without reaching for the mouse.

Result card, Start over and Continue

When all three are chosen, a card shows the city large, with region and country underneath and a country-code badge. Start over clears everything and refocuses the first field. Continue is disabled until the path is complete, then confirms with a short "Saved" state.

Your data

Edit the nested DATA object and the CODES badges, or replace getOptions() with a real request that takes the parent values. Pair this with a country selector when one searchable level is enough, or a multi-select dropdown for multi-value fields.

What's included

Features

Native accessible selects
Real select elements keep keyboard, screen-reader and mobile-picker behaviour, with a custom caret and focus ring.
Downward reset
Changing any level clears and disables every level below it, so invalid combinations cannot be submitted.
Per-level loading state
A spinner replaces the caret while a level loads, and the select stays disabled until its options arrive.
Stale-response guard
A token per level discards responses for a choice that has since changed.
Progress steps
A three-step indicator shows the current step, ticks for finished ones and a filling connector line.
Option counts and focus flow
Each label shows how many options exist, and the newly opened select pulses and takes focus.
Result card and Continue
A card shows the chosen city, region and country; Continue unlocks only when all three are set.
FormData ready
Named, required selects return { country, state, city } from new FormData(form) on submit.

Step by step

How to Use

  1. 1
    Paste HTML, CSS and JSCountry is ready; Region and City are disabled with "Select a country first" style placeholders, and step 1 is highlighted.
  2. 2
    Choose a countryA spinner shows briefly, then the Region list opens with a pulse and keyboard focus, and a hint says how many regions it has.
  3. 3
    Choose a region, then a cityEach choice fills the next level. Finished steps become ticks and the line between them fills in.
  4. 4
    Change a parentPick a different country and Region and City clear and disable again, so a city from the old country can never stay selected.
  5. 5
    Press Continue or Start overContinue is enabled only when all three are chosen and logs { country, state, city } from FormData. Start over clears everything.
  6. 6
    Load real dataReplace getOptions() with a fetch that takes the parent values. The reset and stale-response guard work unchanged.

Real-world uses

Common Use Cases

Checkout and signup addresses
Collect country, region and city before the street address, with the stale-response guard stopping a fast country change from showing the wrong regions.
Vehicle and product finders
Use make, model and trim, or category, subcategory and product, where each list depends on the previous choice and the option count shows how many remain.
Org and team pickers
Choose company, department and team in admin screens, with Start over to clear the whole path and refocus the first field.
Booking and travel forms
Pick region, city and venue, loading each level from an API while the spinner shows progress and the card confirms the final place.
Single-level alternative
When only the country matters, a searchable country selector is faster; this snippet is for forms where later choices depend on earlier ones.

Got questions?

Frequently Asked Questions

Replace the body of getOptions() with a fetch that uses the parent values, for example the selected country to get its regions. load() already shows the spinner, keeps the select disabled until the promise resolves and fills the options, so only that one function changes.

Every reset increases a counter for each cleared level, and load() remembers the value it started with. When a response arrives and the counter has moved on, the visitor has changed the parent in the meantime, so the response is ignored instead of filling the select with options for the wrong parent.

Set the country, wait for its regions to load, set the region, wait for the cities, then set the city. Chain the promises from getOptions() so each level is filled before its value is assigned, and call update() once at the end to refresh the steps and card.

Keep country, state and city in state, load each level in an effect that depends on the parent value, and clear the children in the parent's change handler. Ignore stale responses with a cancelled flag or AbortController. Native selects with controlled values port directly.

Copy & paste

Source Code

<form class="cs-card" id="csForm" novalidate>
  <h2 class="cs-title">Where are you based?</h2>
  <p class="cs-lead">Pick a country, then a region, then a city.</p>

  <ol class="cs-steps" aria-hidden="true">
    <li class="is-now"><b>1</b><span>Country</span></li>
    <li><b>2</b><span>Region</span></li>
    <li><b>3</b><span>City</span></li>
  </ol>

  <div class="cs-field">
    <div class="cs-row"><label class="cs-label" for="csCountry">Country</label><span class="cs-hint"></span></div>
    <div class="cs-wrap">
      <select class="cs-select" id="csCountry" name="country" required></select>
      <span class="cs-spin" aria-hidden="true"></span>
      <svg class="cs-caret" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.5" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg>
    </div>
  </div>

  <div class="cs-field">
    <div class="cs-row"><label class="cs-label" for="csState">State / Region</label><span class="cs-hint"></span></div>
    <div class="cs-wrap">
      <select class="cs-select" id="csState" name="state" required disabled></select>
      <span class="cs-spin" aria-hidden="true"></span>
      <svg class="cs-caret" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.5" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg>
    </div>
  </div>

  <div class="cs-field">
    <div class="cs-row"><label class="cs-label" for="csCity">City</label><span class="cs-hint"></span></div>
    <div class="cs-wrap">
      <select class="cs-select" id="csCity" name="city" required disabled></select>
      <span class="cs-spin" aria-hidden="true"></span>
      <svg class="cs-caret" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.5" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg>
    </div>
  </div>

  <div class="cs-summary" id="csSummary" aria-live="polite"></div>

  <div class="cs-actions">
    <button type="button" class="cs-clear" id="csClear">Start over</button>
    <button type="submit" class="cs-go" id="csGo" disabled>Continue</button>
  </div>
</form>

Extend with AI

Take This Snippet Further With AI

Paste this snippet's HTML, CSS and JS into an AI coding assistant like Claude and ask it to explain why reset(level + 1) must run before load(level + 1), and how the per-level token makes a slow response for an old choice harmless. Since the progress steps, loading spinner, option counts, result card and FormData submit already exist, ask for what is missing: restoring a saved selection by chaining the loads, a fourth level such as neighbourhood, a searchable combobox at each level that keeps the same reset rule, error handling with a Retry link when a request fails, or remembering the last choice in localStorage. It can also review whether auto-focusing the next select is right for touch devices.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a three-level cascading select (country, region, city) in plain HTML, CSS and vanilla JavaScript using native select elements and a form.

Requirements:
- Data is one nested object: country -> region -> array of cities. A getOptions(level, parents) function returns a promise (use a 350ms timeout to imitate an API) so loading can be shown.
- Selects have name and required attributes. Region and City start disabled with placeholders "Select a country first" and "Select a region first".
- Changing any select must first reset every level below it (clear options, restore the waiting placeholder, disable, clear its hint, cancel in-flight requests by bumping a per-level token), then load the next level if a value is chosen.
- While a level loads, show "Loading…" in the select and a spinner in place of the caret. When the response arrives, ignore it if the level's token has changed; otherwise fill the options, enable the select, show "N regions/cities" as a hint, pulse the field and move focus to it.
- Above the fields show three progress steps (Country, Region, City): the first unfinished step is highlighted, finished steps become ticks and the connector line between them fills.
- Below the fields show a summary card: a neutral message when nothing is chosen, a "A › B …" breadcrumb when partial, and when complete a country-code badge with the city in bold and "Region, Country" underneath. Build it with DOM text nodes, not innerHTML with data.
- Add a "Start over" button that clears everything and refocuses the country select, and a Continue button that is disabled until all three are chosen; on submit prevent the default, read FormData and briefly show "Saved ✓".
- Respect prefers-reduced-motion and give every control a visible focus style.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.