Component Library

← Back to gallery

Select with Other revealing text input in HTML, CSS and vanilla JS

A copy-paste Select with Other revealing text input in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Forms · Tags: select field, other, conditional, text input, form

Live preview

Source (copy-ready)

<section class="example-select-field-other-reveal" data-dcl-scheme="light" aria-labelledby="example-select-field-other-reveal-title">
<header><h2 id="example-select-field-other-reveal-title">Select with Other revealing text input</h2>
<p>Choosing Other reveals a required free-text explanation without leaving the select.</p></header>
<div class="parts">
<div class="field"><label for="example-select-field-other-reveal-reason">Reason for contact</label><select id="example-select-field-other-reveal-reason" data-select><option>Billing</option><option>Technical support</option><option>Other</option></select></div>
<div class="other-field" data-other hidden><label for="example-select-field-other-reveal-other">Describe the other reason</label><input type="text" id="example-select-field-other-reveal-other" data-input disabled></div><p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
  const root = document.currentScript.closest(".example-select-field-other-reveal");
  const instance = Array.from(document.getElementsByClassName("example-select-field-other-reveal")).indexOf(root);
  if (instance > 0) {
    const singleReferences = ["id", "aria-activedescendant", "for", "list", "name"];
    const tokenReferences = ["aria-labelledby", "aria-controls", "aria-describedby"];
    for (const element of [root, ...root.querySelectorAll("*")]) {
      for (const attr of singleReferences) {
        if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr) + "--" + instance);
      }
      for (const attr of tokenReferences) {
        if (element.hasAttribute(attr)) {
          const rewritten = element.getAttribute(attr).trim().split(/\s+/).filter(Boolean).map((reference) => reference + "--" + instance);
          element.setAttribute(attr, rewritten.join(" "));
        }
      }
      const href = element.getAttribute("href");
      if (href && href.startsWith("#")) element.setAttribute("href", href + "--" + instance);
    }
  }
  const preference = matchMedia("(prefers-color-scheme: dark)");
  const apply = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
  preference.addEventListener("change", apply);
  apply();
  const select=root.querySelector('[data-select]'),wrap=root.querySelector('[data-other]'),input=root.querySelector('[data-input]'),status=root.querySelector('[data-status]');
  function sync(){const other=select.value==='Other';wrap.hidden=!other;input.disabled=!other;input.required=other;status.textContent=other?'Please describe the other reason.':'No extra explanation required.';if(other)input.focus()}select.addEventListener('change',sync);sync();
})();
</script></section>
<style>
.example-select-field-other-reveal { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-select-field-other-reveal a { color: #005c62; }
.example-select-field-other-reveal button, .example-select-field-other-reveal summary, .example-select-field-other-reveal input, .example-select-field-other-reveal select, .example-select-field-other-reveal textarea {
  color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-select-field-other-reveal button:hover, .example-select-field-other-reveal summary:hover, .example-select-field-other-reveal button[aria-selected="true"], .example-select-field-other-reveal button[aria-pressed="true"] {
  color: #ffffff; background-color: #00535a;
}
.example-select-field-other-reveal dialog { color: #11292f; background-color: #ffffff; }
.example-select-field-other-reveal [data-note] { color: #11292f; background-color: #e4edef; }
.example-select-field-other-reveal [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-select-field-other-reveal [data-success] { color: #14532d; background-color: #dcfce7; }
.example-select-field-other-reveal [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-select-field-other-reveal :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }

.example-select-field-other-reveal[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-select-field-other-reveal[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-select-field-other-reveal[data-dcl-scheme="dark"] button, .example-select-field-other-reveal[data-dcl-scheme="dark"] summary,
.example-select-field-other-reveal[data-dcl-scheme="dark"] input, .example-select-field-other-reveal[data-dcl-scheme="dark"] select, .example-select-field-other-reveal[data-dcl-scheme="dark"] textarea {
  color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-select-field-other-reveal[data-dcl-scheme="dark"] button:hover, .example-select-field-other-reveal[data-dcl-scheme="dark"] summary:hover,
.example-select-field-other-reveal[data-dcl-scheme="dark"] button[aria-selected="true"], .example-select-field-other-reveal[data-dcl-scheme="dark"] button[aria-pressed="true"] {
  color: #11292f; background-color: #8fe1da;
}
.example-select-field-other-reveal[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-select-field-other-reveal[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-select-field-other-reveal[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-select-field-other-reveal[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-select-field-other-reveal[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-select-field-other-reveal[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }

.example-select-field-other-reveal, .example-select-field-other-reveal * { box-sizing: border-box; min-width: 0; }
.example-select-field-other-reveal { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-select-field-other-reveal h2, .example-select-field-other-reveal h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-select-field-other-reveal header p, .example-select-field-other-reveal p { margin-block: 0.5rem; }
.example-select-field-other-reveal ul, .example-select-field-other-reveal ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-select-field-other-reveal dl { margin-block: 0.5rem; }
.example-select-field-other-reveal dt { font-weight: 700; }
.example-select-field-other-reveal dd { margin: 0 0 0.6rem; }
.example-select-field-other-reveal fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-select-field-other-reveal legend { padding-inline: 0.25rem; font-weight: 700; }
.example-select-field-other-reveal button, .example-select-field-other-reveal input, .example-select-field-other-reveal select, .example-select-field-other-reveal textarea {
  min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-select-field-other-reveal input, .example-select-field-other-reveal select, .example-select-field-other-reveal textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-select-field-other-reveal input[type="checkbox"], .example-select-field-other-reveal input[type="radio"] {
  display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-select-field-other-reveal input[type="file"] { padding: 0.45rem; }
.example-select-field-other-reveal input[type="range"] { padding-inline: 0; }
.example-select-field-other-reveal textarea { min-block-size: 6rem; resize: vertical; }
.example-select-field-other-reveal button, .example-select-field-other-reveal summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-select-field-other-reveal button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-select-field-other-reveal table { border-collapse: collapse; inline-size: 100%; }
.example-select-field-other-reveal th, .example-select-field-other-reveal td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-select-field-other-reveal [hidden] { display: none !important; }
.example-select-field-other-reveal [data-note], .example-select-field-other-reveal [data-error], .example-select-field-other-reveal [data-success], .example-select-field-other-reveal [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-select-field-other-reveal .parts { display: grid; gap: 1rem; }
.example-select-field-other-reveal .field, .example-select-field-other-reveal .fields { display: grid; gap: 0.4rem; }
.example-select-field-other-reveal .fields { gap: 0.75rem; }
.example-select-field-other-reveal .actions, .example-select-field-other-reveal .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-select-field-other-reveal .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-select-field-other-reveal .plain-list { margin: 0; padding: 0; list-style: none; }
.example-select-field-other-reveal .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-select-field-other-reveal .choice input { margin-block-start: 0.25rem; }
.example-select-field-other-reveal .choice-stack { display: grid; gap: 0.55rem; }
.example-select-field-other-reveal .tile { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0.55rem; align-items: start; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-select-field-other-reveal .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-select-field-other-reveal .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-select-field-other-reveal .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-select-field-other-reveal .hint { font-size: 0.875rem; }
.example-select-field-other-reveal .status { min-block-size: 1.5rem; margin-block: 0; }
.example-select-field-other-reveal .two-column, .example-select-field-other-reveal .split-layout { display: grid; gap: 1rem; }
.example-select-field-other-reveal .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-select-field-other-reveal .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-select-field-other-reveal .visually-hidden {
  position: absolute; inline-size: 1px; block-size: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}
@media (min-width: 34rem) {
  .example-select-field-other-reveal .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .example-select-field-other-reveal .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
  .example-select-field-other-reveal { padding: 0.75rem; }
  .example-select-field-other-reveal .actions, .example-select-field-other-reveal .inline-row { align-items: stretch; }
  .example-select-field-other-reveal .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-select-field-other-reveal * { transition: none !important; } }
.example-select-field-other-reveal .other-field[hidden] { display: none !important; }

.example-select-field-other-reveal .other-field{display:grid;gap:.4rem;margin-block-start:.75rem}
</style>

When to use

Use when a controlled list covers most cases but a small escape hatch is necessary. Require the explanation only while Other remains selected.

Avoid

Do not hide important common choices behind Other merely to shorten the list.

Accessibility

The native select and revealed input are both visibly labelled. Hidden content uses the hidden attribute, the input is disabled and not required until revealed, and state changes are announced politely.

Was this useful? Send feedback.