0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Show or hide a form section based on other fields' values. Hidden sections get hidden + inert and their controls are disabled, so hidden required fields never block submit and hidden values are not sent.
Overview
data-show-when shows an element only while a condition on other form fields is true; data-hide-when is the inverse. Use them for sections that only apply to some answers: business details for a business account, a shipping address when delivery is chosen.
A hidden section gets the hidden and inert attributes, and every enabled form control inside it is disabled and marked data-conditional-disabled. Disabling is what matters for forms: disabled controls are barred from constraint validation, so a hidden required field never blocks submit, and their values are not submitted. When the section is shown again, only the controls it disabled are re-enabled; controls you disabled yourself stay disabled.
Without JavaScript every section is visible and every control is enabled, so the whole form still works.
Usage
Name the controlling field and the value that reveals the section.
Put the conditional section directly after the field that controls it. Revealed content is not announced, so DOM order is what lets keyboard and screen reader users find it next.
The element is shown while the condition is true, hidden otherwise.
data-hide-when
condition
—
The element is hidden while the condition is true. Ignored when data-show-when is also present.
data-conditional-init
boolean
set by script
Marks a wired section. Read-only; a clone that carries it is still wired.
data-conditional-disabled
boolean
set by script
On each control the script disabled. Only these are re-enabled on show.
Condition syntax
The same language as data-wizard-if; = and : are interchangeable.
Syntax
True when
Example
name
The field has a value: non-empty text, a selected option, a checked box.
data-show-when="newsletter"
!name
The field has no value.
data-show-when="!newsletter"
name=value or name:value
One of the field's values equals value.
data-show-when="delivery=ship"
name!=value or name:!value
None of the field's values equals value.
data-show-when="plan!=free"
a && b
Both terms are true. Binds tighter than ||.
data-show-when="role=admin && plan=pro"
a || b
Either term is true.
data-show-when="role=admin || role=editor"
What counts as a field value
Text-like inputs, textarea, single select: the current value; empty is "no value".
Radio group: the checked radio's value.
Checkbox: its value while checked (a checkbox without a valueattribute is "on", so name=true never matches; use the bare name form).
Checkbox group (several boxes, one name): every checked value. topics=sports matches whenever the Sports box is checked, whatever its position.
select[multiple]: every selected value.
Form-associated components (combo-box, date-picker, star-rating and other components that submit a value): their value.
Scope
Inside a <form>, names resolve through form.elements, so controls attached with form="id" count and other forms on the page do not. A section outside any form looks the name up in the whole document.
Limits
Comparisons are exact string matches; there are no numeric or pattern operators.
Conditions re-run on input and change. If script sets a control's .value, dispatch an input event afterwards (el.dispatchEvent(new Event('input', { bubbles: true }))).
Changing the data-show-whenattribute itself does not re-evaluate the section until the next field change.
Behaviour
When it runs: every section is evaluated on load and when it is inserted later (fetched fragments, cloned templates, forms added after load). One capture-phase listener on the document handles every form and every section outside a form.
Reset: after form.reset() or a reset button, sections re-evaluate against the restored values. A reset cancelled with preventDefault() changes nothing.
Nesting: a control inside two hidden sections is re-enabled only when neither is hidden. Sections inside a hidden data-wizard-if step follow the same rule.
Events
None. The attribute listens to the form's own input, change and reset events.
Styling
The shipped rule (in form-enhancements.css) makes sure an author display value cannot override hidden:
There are no custom properties or parts. Because the section switches between display: none and its normal display, a plain opacity transition never runs. To animate the reveal, use @starting-style:
Hidden sections are removed from the accessibility tree and the tab order (hidden + inert), so nobody lands in an invisible field.
Their controls are disabled, so validation never points at a field the user cannot see.
Revealing a section is not announced and does not move focus. Place the section right after its trigger so it is the next thing users reach.
The trigger gets no aria-expanded / aria-controls. If the reveal is a user-driven disclosure rather than a consequence of an answer, use <details> or data-toggle instead.
Without JavaScript all sections are visible, so no content or field is lost.
Examples
Hide when (inverse)
The field is visible by default and hides when the box is checked.
Hide a field when a checkbox is checked
123456789
<label><inputtype="checkbox"name="gift"value="yes"> This is a gift</label><form-fielddata-hide-when="gift=yes"><labelfor="receipt">Send receipt to</label><inputtype="email"id="receipt"name="receipt"></form-field>
Radio group
The address is required only while "Ship" is chosen; with "Pickup" the hidden field is disabled and the form submits.
Leave out the value to show the section whenever the field has one: a checked box, a non-empty input, a selected option.
Truthy check with a checkbox
123456789101112
<label><inputtype="checkbox"name="newsletter"> Subscribe to newsletter</label><form-fielddata-show-when="newsletter"><labelfor="freq">Frequency</label><selectid="freq"name="frequency"><option>Weekly</option><option>Monthly</option></select></form-field>
Checkbox group and compound conditions
A term matches when any checked box in the group has the value; && needs both.
Checkbox group with AND
12345678910111213141516
<fieldset><legend>Topics</legend><label><inputtype="checkbox"name="topics"value="news"> News</label><label><inputtype="checkbox"name="topics"value="sports"> Sports</label><label><inputtype="checkbox"name="topics"value="weather"> Weather</label></fieldset><form-fielddata-show-when="topics=sports"><labelfor="team">Favourite team</label><inputtype="text"id="team"name="team"></form-field><form-fielddata-show-when="topics=news && topics=weather"><labelfor="region">Region for news and weather</label><inputtype="text"id="region"name="region"></form-field>
Related
Use this
When
data-show-when
A section applies only for some answers to other fields, and its fields must not block or join the submission while hidden.