Stimulus controller for dependent select dropdowns

4738
0

Dependent dropdowns are a classic UX pattern where one select populates based on another's value. With Stimulus, I handle this entirely client-side after the initial page load by storing options as data attributes or fetching them via AJAX. When the parent select changes, the controller filters or fetches options for the child select. This keeps forms responsive without server round-trips for every interaction. I use data-<controller>-<value-name>-value to pass available options as JSON from the server. For large datasets, I switch to fetching options via fetch() to a JSON endpoint. The pattern extends to any cascading form field scenario: country → state → city, category → subcategory, etc.