StealThis .dev

Form — Conditional show/hide fields

A business-account form that grows with the answers it receives. Saying you have a company reveals company-name and team-size fields, unchecking same-as-billing slides open a shipping address, and picking Other on the referral question exposes a free-text note. Revealed blocks animate in, become required only while visible, and the validation pass skips anything hidden — so users never get blocked by fields they cannot see, with an accessible error summary on submit.

Open in Lab
html css vanilla-js
Targets: JS HTML

Code

Conditional show/hide fields

A business-account signup that only asks for what your answers require. Two questions and one checkbox act as gates: choosing “Yes, a company” reveals company name and team size; clearing “Shipping address is the same as billing” slides open a shipping field; and selecting “Other” on the referral dropdown exposes a short free-text note. Each gated block uses a CSS grid-template-rows 0fr → 1fr transition so content expands smoothly, with a fallback path for reduced-motion users.

The trick is that hidden fields are inert. When a block is collapsed its controls are disabled, lose their required/aria-required flags, and are cleared of any error or success styling. The submit pass only inspects controls that are currently enabled, so a user is never blocked by a field they cannot see; the moment a block opens, its fields become required again and validate live on blur and input.

A failed submit builds a role="alert" error summary listing each problem as an in-page anchor — clicking one focuses and scrolls to the field. The radio group, checkbox, and selects are fully keyboard-operable with visible focus rings, status is announced through an aria-live toast, and a successful submit shows an animated confirmation that names exactly which optional sections were filled in, plus a “Start a new request” action that resets every gate.