Show If Web Component Demo

A web component that conditionally shows or hides form fields based on the values of other fields. See the README for installation and API documentation.

Use the labeled controls to reveal dependent fields. Revealing a field does not move focus. Without JavaScript or if the module fails to load, all authored fields remain available. Custom-class examples use visible availability text and solid/dashed outlines; green/red accents are supplementary.

Basic Conditional Show/Hide

Simple conditions that show a field when another field has any value.

Show Field if Another Field Has Any Value

Using conditions="email=*" shows the field when the email field contains any value.

<label>Email address
  <input type="email" name="email" />
</label>

<form-show-if 
  conditions="email=*">
  <label for="phone">Phone number
    <input 
      name="phone" 
      id="phone" 
      type="tel" />
  </label>
</form-show-if>

OR Logic

Show a field when any of multiple conditions are met using the || operator.

Multiple Conditions (OR)

Using conditions="email=*||choose=3" shows the field when either the email field has a value OR the select has "Option 3" chosen.

<label>Email address
  <input type="email" name="email" />
</label>

<label for="choose">Choose an option
  <select name="choose" id="choose">
    <option value="">Select...</option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
  </select>
</label>

<form-show-if 
  conditions="email=*||choose=3">
  <label for="details">Details
    <input name="details" id="details" />
  </label>
</form-show-if>

Specific Value Matching

Show a field only when another field matches an exact value.

Exact Value Match

Using conditions="email=foo@bar.tld" shows the field only when the email field contains that exact value.

<label>Email address
  <input type="email" name="email" />
</label>

<form-show-if 
  conditions="email=foo@bar.tld">
  <label for="secret">Secret code
    <input name="secret" id="secret" />
  </label>
</form-show-if>

Radio & Checkbox Integration

Common pattern for "Other" options in radio and checkbox groups.

Radio "Other" Pattern

When a user selects "Other", a text field appears for them to specify.

<fieldset>
  <legend>Choose an option</legend>
  <label>
    <input type="radio" 
      name="radio" value="1" />
    Option 1
  </label>
  <label>
    <input type="radio" 
      name="radio" value="2" />
    Option 2
  </label>
  <div class="other-radio">
    <label>
      <input type="radio" 
        name="radio" value="other" />
      Other
    </label>
    <form-show-if 
      conditions="radio=other">
      <label class="dependent">
        <span class="visibly-hidden">Other value</span>
        <input name="radio-other" />
      </label>
    </form-show-if>
  </div>
</fieldset>
Choose an option

Checkbox "Other" Pattern

Similar to radio, but works with checkbox arrays using checkbox[]=other syntax.

<fieldset>
  <legend>Choose options</legend>
  <label>
    <input type="checkbox" 
      name="checkbox[]" value="1" />
    Option 1
  </label>
  <label>
    <input type="checkbox" 
      name="checkbox[]" value="2" />
    Option 2
  </label>
  <div class="other-checkbox">
    <label>
      <input type="checkbox" 
        name="checkbox[]" 
        value="other" />
      Other
    </label>
    <form-show-if 
      conditions="checkbox[]=other">
      <label class="dependent">
        <span class="visibly-hidden">Other value</span>
        <input name="checkbox-other" />
      </label>
    </form-show-if>
  </div>
</fieldset>
Choose options

Custom CSS Classes

Use enabled-class to style available fields. Providing disabled-class replaces native hiding with a class; unless your CSS hides it, the unavailable field stays visible but disabled and is excluded from submission.

<style>
.enabled label,
[disabled-class='disabled'] label,
.disabled label {
  outline: 2px solid currentColor;
  outline-offset: 0.25rem;
  background: Canvas;
  color: CanvasText;
  border-inline-start: 0.25rem solid #17652e;
  padding: 0.75rem;
}
.disabled label {
  outline-style: dashed;
  border-inline-start-color: #982b39;
}
.state-off,
.disabled .state-on {
  display: none;
}
.disabled .state-off {
  display: inline;
}
</style>

Available fields use a solid outline. Unavailable fields use a dashed outline and native disabled behavior. The fields below have authored status descriptions, which you can translate or replace. This CSS does not generate labels or messages.

Enabled Class

The enabled-class attribute adds a CSS class when the field should be visible, like enabled-class="enabled".

<form-show-if 
  conditions="email=*" 
  enabled-class="enabled">
  <label for="company">Company
    <input name="company" id="company"
      aria-describedby="company-state" />
  </label>
  <small id="company-state" class="state-on">Available</small>
</form-show-if>
Available

Disabled Class

The disabled-class attribute adds a CSS class when conditions do not match, like disabled-class="disabled".

<form-show-if 
  conditions="email=*"
  disabled-class="disabled">
  <label for="department">Department
    <input 
      name="department" 
      id="department" aria-describedby="department-state" />
  </label>
  <small id="department-state">
    <span class="state-on">Available</span>
    <span class="state-off">Unavailable: enter an email above</span>
  </small>
</form-show-if>
Available Unavailable: enter an email above

Both Classes

Combine both classes to style available and unavailable fields without native hiding.

<form-show-if 
  conditions="email=*"
  enabled-class="enabled"
  disabled-class="disabled">
  <label for="notes">Notes
    <textarea 
      name="notes" 
      id="notes" aria-describedby="notes-state"></textarea>
  </label>
  <small id="notes-state">
    <span class="state-on">Available</span>
    <span class="state-off">Unavailable: enter an email above</span>
  </small>
</form-show-if>
Available Unavailable: enter an email above

Label Structure Variations

The component works with different label / input markup patterns.

Sibling Label & Input

label and input as siblings instead of the input being nested in the label.

<form-show-if 
  conditions="email=*">
  <label for="field1">
    Field label
  </label>
  <input 
    type="text" 
    id="field1" 
    name="field1" />
</form-show-if>

Label with Nested Elements

Works with labels containing one or more nested elements.

<form-show-if 
  conditions="email=*">
  <label for="field2">
    <span>Multi-part</span>
    <span>label text</span>
  </label>
  <input id="field2" name="field2" />
</form-show-if>

Label Wrapping Input

Label can wrap the input element.

<form-show-if 
  conditions="email=*">
  <label>
    <span>Wrapped input</span>
    <input name="field3" />
  </label>
</form-show-if>

Complex Examples

More advanced use cases combining multiple techniques.

Multiple Fields, One Controller

Keep the controlling select available and give each dependent field its own condition. Conditions compare values, not the controller’s disabled state; hiding one field does not automatically disable its dependents.

<label for="feature-type">Feature type
  <select name="feature-type" id="feature-type">
    <option value="">Select...</option>
    <option value="basic">Basic</option>
    <option value="advanced">Advanced</option>
  </select>
</label>

<form-show-if 
  conditions="feature-type=*">
  <label for="feature-summary">Feature summary
    <input name="feature-summary" id="feature-summary" />
  </label>
</form-show-if>

<form-show-if 
  conditions="feature-type=advanced">
  <label for="config">
    Advanced config
    <textarea 
      name="config" 
      id="config"></textarea>
  </label>
</form-show-if>

Real-World Example

A complete contact form demonstrating practical usage.

Contact Form with Conditional Fields

This form collects contact preferences and conditionally shows contact detail fields.

<form action="#">
  <label>Name
    <input type="text" 
      name="name" required />
  </label>

  <fieldset>
    <legend>
      How should we contact you?
    </legend>
    <label>
      <input type="checkbox" 
        name="contact[]" 
        value="email" />
      Email
    </label>
    <label>
      <input type="checkbox" 
        name="contact[]" 
        value="phone" />
      Phone
    </label>
  </fieldset>

  <form-show-if 
    conditions="contact[]=email">
    <label for="contact-email">
      Email address
      <input type="email" 
        name="contact-email" 
        id="contact-email" />
    </label>
  </form-show-if>

  <form-show-if 
    conditions="contact[]=phone">
    <label for="contact-phone">
      Phone number
      <input type="tel" 
        name="contact-phone" 
        id="contact-phone" />
    </label>
  </form-show-if>

  <label for="message">Message
    <textarea name="message" 
      id="message" 
      required></textarea>
  </label>

  <button type="submit">
    Send
  </button>
  <button type="reset">Reset</button>
</form>
How should we contact you?

API Reference

For complete documentation of attributes, behavior, and browser support, see the README.