Bootstrap Conditional Form Fields — Free HTML CSS JS Snippet
Bootstrap Conditional Form Fields · Forms · Plain HTML, CSS & JS · Live preview
Copy & paste
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>.bscond-card { width: 360px; max-width: 100%; border: 1px solid #eceef1; border-radius: 14px; }const type = document.getElementById('bscondType');
const businessFields = document.getElementById('bscondBusinessFields');
const nonprofitFields = document.getElementById('bscondNonprofitFields');
const einField = document.getElementById('bscondEinField');
const registered = document.getElementById('bscondRegistered');
function update() {
const v = type.value;
businessFields.classList.toggle('d-none', v !== 'business');
nonprofitFields.classList.toggle('d-none', v !== 'nonprofit');
// A second-level condition: the EIN field only shows for a nonprofit that
// has also checked "registered 501(c)(3)" — nested conditional logic, not
// just a flat one-field-per-option mapping.
einField.classList.toggle('d-none', !(v === 'nonprofit' && registered.checked));
}
type.addEventListener('change', update);
registered.addEventListener('change', update);
update();Extend with AI
Take This Snippet Further With AI
The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.
What's included
Features
About this UI Snippet
Bootstrap Conditional Form Fields — HTML, CSS & JavaScript

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.
Step by step
How to Use
- 1Load the snippetOnly the type dropdown shows; no additional fields are visible yet.
- 2Select "Business"Company name and Tax ID fields appear immediately.
- 3Switch to "Nonprofit"The business fields disappear and organization name plus a registered-status checkbox appear instead.
- 4Check "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.
- 5Uncheck the box, or switch back to "Personal"The EIN field (and every conditional group) disappears again immediately.
Real-world uses
Common Use Cases
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.