Source Code

<div class="container py-5 d-flex justify-content-center">
  <div class="card bscond-card">
    <div class="card-body p-4">
      <label class="form-label small fw-semibold">How do you plan to use this?</label>
      <select class="form-select mb-3" id="bscondType">
        <option value="">Select one...</option>
        <option value="personal">Personal</option>
        <option value="business">Business</option>
        <option value="nonprofit">Nonprofit</option>
      </select>

      <div class="bscond-group d-none" id="bscondBusinessFields">
        <label class="form-label small fw-semibold">Company name</label>
        <input type="text" class="form-control mb-2" id="bscondCompany">
        <label class="form-label small fw-semibold">Tax ID</label>
        <input type="text" class="form-control mb-3" id="bscondTaxId">
      </div>

      <div class="bscond-group d-none" id="bscondNonprofitFields">
        <label class="form-label small fw-semibold">Organization name</label>
        <input type="text" class="form-control mb-2" id="bscondOrgName">
        <div class="form-check mb-3">
          <input class="form-check-input" type="checkbox" id="bscondRegistered">
          <label class="form-check-label small" for="bscondRegistered">We're a registered 501(c)(3)</label>
        </div>
      </div>

      <div class="bscond-group d-none" id="bscondEinField">
        <label class="form-label small fw-semibold">EIN</label>
        <input type="text" class="form-control mb-3" id="bscondEin">
      </div>

      <button type="button" class="btn btn-dark btn-sm fw-bold w-100" id="bscondSubmit">Continue</button>
    </div>
  </div>
</div>

Bootstrap Conditional Form Fields — Free HTML CSS JS Snippet

Bootstrap Conditional Form Fields · Forms · Plain HTML, CSS & JS · Live preview

What's included

Features

One update() function is the single source of truth for every field's visibility
A genuinely nested condition — one field requires two separate inputs to both be true, not just one
Every relevant control (the dropdown and the checkbox) triggers the same shared update function
Visibility is correctly computed on initial load too, not only after the first user interaction
Switching away from a selection cleanly hides its fields rather than leaving them stranded visible

About this UI Snippet

Bootstrap Conditional Form Fields — HTML, CSS & JavaScript

Screenshot of the Bootstrap Conditional Form Fields snippet rendered live

A single update() function is the only place field visibility is decided, called from every input that could affect it — the type <select>'s change event and the "registered 501(c)(3)" checkbox's own change event both trigger the same function, rather than each control owning its own separate show/hide logic that could get out of sync with the others.

The business and nonprofit field groups are straightforward one-condition toggles (v !== 'business' hides the business group), but the EIN field demonstrates something a flat "one field shows per dropdown option" implementation can't: it only appears when v === 'nonprofit' && registered.checked are *both* true — selecting Nonprofit alone isn't enough, and checking "registered" while some other type is selected isn't enough either. That's a genuinely nested condition, not just a wider switch statement.

Running update() unconditionally on load (not just after a user interacts) means the form's visible fields always match its actual current state from the very first render — important for a form that might load pre-filled with existing data rather than starting from a blank selection every time.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Hand this snippet to an AI coding assistant like Claude and ask it to preserve values typed into a field group when it's hidden and later re-shown (rather than the user needing to retype them), or to add form validation that only requires fields belonging to the currently visible groups, ignoring hidden ones entirely.

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 Bootstrap 5.3 form with conditional field groups, using the real Bootstrap CDN framework (bootstrap.min.css and bootstrap.bundle.min.js), not custom CSS made to resemble it.

Requirements:
- A dropdown selecting an account type (e.g. Personal, Business, Nonprofit) that shows a different group of additional fields depending on the selection, hiding every other group.
- One additional field must depend on two separate conditions being true simultaneously — e.g. only appearing when a specific type is selected AND a related checkbox elsewhere in the form is also checked, not from the dropdown selection alone.
- Drive all field visibility from a single shared update function triggered by every relevant control's change event, not separate show/hide logic scattered per control.
- Ensure field visibility is correct immediately on page load, not only after the first user interaction, and that switching away from a selection correctly hides fields that were previously shown.

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.

Step by step

How to Use

  1. 1
    Load the snippetOnly the type dropdown shows; no additional fields are visible yet.
  2. 2
    Select "Business"Company name and Tax ID fields appear immediately.
  3. 3
    Switch to "Nonprofit"The business fields disappear and organization name plus a registered-status checkbox appear instead.
  4. 4
    Check "We're a registered 501(c)(3)"An EIN field appears — it only shows because both the type is Nonprofit and this box is checked.
  5. 5
    Uncheck the box, or switch back to "Personal"The EIN field (and every conditional group) disappears again immediately.

Real-world uses

Common Use Cases

Account type or signup forms needing different fields per plan
A common real pattern — personal vs. business vs. nonprofit accounts genuinely need different data collected.
Settings and preference forms with dependent options
Show an advanced option only once a related toggle or mode is enabled, following the same nested-condition approach.
CART
Checkout forms with conditional billing details
Show a tax-exempt ID field only for business customers who've also indicated exempt status, mirroring the nonprofit/EIN pattern here.

Got questions?

Frequently Asked Questions

An EIN is only relevant to a registered nonprofit specifically, not every nonprofit selection generally — modeling it as a single field tied only to the dropdown would either show it prematurely (before registration status is known) or require duplicating the field inside both branches.

Both the nonprofit fields and the EIN field hide immediately, since einField's visibility explicitly requires the type to be nonprofit — the checkbox being checked alone is never sufficient on its own.

Yes. Keep the type and registered values in component state, and compute each group's visibility as a derived boolean expression in the render function — no direct classList manipulation needed, since the framework handles conditional rendering natively.

Add another boolean condition to whichever field's visibility should depend on it, following the same pattern the EIN field already uses — update() has no fixed depth limit built into its structure.