Searchable Select Combobox
A select with a long list you can filter by typing, with arrow keys, highlighted matches and a no-results message.
AI prompt
Clean UI mockup of a country selector: a text field with the letters ger typed and a chevron, below it an open list with three matches Germany with the first letters highlighted in bold, Nigeria and Algeria with the matching letters bold inside, the first row highlighted, a small count 3 results, and below the field another state showing the text No countries found, light theme, flat modern style. Negative: heavy shadows, clutter, text errors, device frames. Reduced motion: static design.About this pattern
Definition
A plain select with dozens of options is slow to use. A combobox lets people type to narrow the list, then pick with the mouse or keyboard. The matching part of each option is highlighted, an empty result says so, and the chosen value stays visible in the field.
Purpose & UX principles
- Use an input with role combobox, aria-expanded, aria-controls and aria-activedescendant; the list has role listbox.
- Filter as you type, case-insensitive, matching from the start of words first.
- Arrow keys move through matches, Enter selects, Escape closes and restores the last value.
- Show a clear no-results message and keep the field usable.
Interaction states
- Type part of a country name, use the arrow keys and Enter.
Motion specification
- The list opens in 120ms.
- Reduced motion: no fade.
Responsive behaviour
- Keep focus in the input while the list is open.
- Announce the number of results for screen readers.
Code
<div class="bx"><label class="fl" for="cb">Country</label><div class="wr"><input id="cb" type="text" role="combobox" aria-expanded="false" aria-controls="lb" aria-autocomplete="list" autocomplete="off" placeholder="Start typing…"><ul id="lb" class="lb" role="listbox" aria-label="Countries" hidden></ul></div><p class="out" id="out" role="status"></p></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.fl{font-size:12px;font-weight:600}.wr{position:relative;margin-top:4px}#cb{width:100%;box-sizing:border-box;height:38px;border:1px solid var(--line);border-radius:10px;padding:0 12px;font:inherit;font-size:14px;background:var(--surface);color:var(--text)}.lb{position:absolute;left:0;right:0;top:calc(100% + 4px);max-height:112px;overflow:auto;list-style:none;background:#fff;border:1px solid var(--line);border-radius:10px;box-shadow:0 8px 20px rgba(0,0,0,.12);padding:4px;z-index:5;animation:fi .12s}.lb li{padding:6px 9px;border-radius:7px;font-size:13px;cursor:pointer}.lb li[aria-selected=true]{background:#171717;color:#fff}.lb li mark{background:none;color:inherit;font-weight:800;text-decoration:underline}.lb li.no{color:var(--muted);cursor:default}@keyframes fi{from{opacity:0}}.out{font-size:12px;color:var(--muted);min-height:16px;margin:0}@media (prefers-reduced-motion:reduce){.lb{animation:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const C=['Algeria','Argentina','Australia','Brazil','Canada','Chile','Denmark','Egypt','France','Germany','Ghana','India','Italy','Japan','Kenya','Mexico','Nigeria','Norway','Peru','Spain'],cb=document.getElementById('cb'),lb=document.getElementById('lb'),out=document.getElementById('out');let list=[],act=0,last='';
const esc=s=>s.replace(/&/g,'&').replace(/</g,'<');
function hl(n,q){if(!q)return esc(n);const i=n.toLowerCase().indexOf(q.toLowerCase());return i<0?esc(n):esc(n.slice(0,i))+'<mark>'+esc(n.slice(i,i+q.length))+'</mark>'+esc(n.slice(i+q.length));}
function render(){const q=cb.value.trim().toLowerCase();list=C.filter(c=>c.toLowerCase().includes(q)).sort((a,b)=>(b.toLowerCase().startsWith(q)?1:0)-(a.toLowerCase().startsWith(q)?1:0));act=Math.min(act,Math.max(0,list.length-1));lb.hidden=false;cb.setAttribute('aria-expanded','true');
lb.innerHTML=list.length?list.map((c,i)=>'<li role="option" id="o'+i+'" data-i="'+i+'" aria-selected="'+(i===act)+'">'+hl(c,q)+'</li>').join(''):'<li class="no" role="option" aria-selected="false">No countries found</li>';if(list.length){cb.setAttribute('aria-activedescendant','o'+act);const el=document.getElementById('o'+act);if(el)el.scrollIntoView({block:'nearest'});}else cb.removeAttribute('aria-activedescendant');out.textContent=list.length+' result'+(list.length===1?'':'s')+'.';}
function close(){lb.hidden=true;cb.setAttribute('aria-expanded','false');cb.removeAttribute('aria-activedescendant');}
function pick(i){const c=list[i];if(!c)return;cb.value=c;last=c;close();out.textContent='Selected '+c+'.';}
cb.addEventListener('input',()=>{act=0;render();});cb.addEventListener('focus',render);
cb.addEventListener('keydown',e=>{if(e.key==='ArrowDown'){e.preventDefault();if(lb.hidden)render();else{act=(act+1)%Math.max(1,list.length);render();}}else if(e.key==='ArrowUp'&&!lb.hidden){e.preventDefault();act=(act-1+list.length)%Math.max(1,list.length);render();}else if(e.key==='Enter'&&!lb.hidden){e.preventDefault();pick(act);}else if(e.key==='Escape'&&!lb.hidden){e.preventDefault();cb.value=last;close();}});
lb.addEventListener('mousedown',e=>{const li=e.target.closest('li[data-i]');if(li){e.preventDefault();pick(+li.dataset.i);}});cb.addEventListener('blur',()=>setTimeout(()=>{close();if(cb.value&&!C.includes(cb.value))cb.value=last;},100));One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Searchable Select Combobox — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.fl{font-size:12px;font-weight:600}.wr{position:relative;margin-top:4px}#cb{width:100%;box-sizing:border-box;height:38px;border:1px solid var(--line);border-radius:10px;padding:0 12px;font:inherit;font-size:14px;background:var(--surface);color:var(--text)}.lb{position:absolute;left:0;right:0;top:calc(100% + 4px);max-height:112px;overflow:auto;list-style:none;background:#fff;border:1px solid var(--line);border-radius:10px;box-shadow:0 8px 20px rgba(0,0,0,.12);padding:4px;z-index:5;animation:fi .12s}.lb li{padding:6px 9px;border-radius:7px;font-size:13px;cursor:pointer}.lb li[aria-selected=true]{background:#171717;color:#fff}.lb li mark{background:none;color:inherit;font-weight:800;text-decoration:underline}.lb li.no{color:var(--muted);cursor:default}@keyframes fi{from{opacity:0}}.out{font-size:12px;color:var(--muted);min-height:16px;margin:0}@media (prefers-reduced-motion:reduce){.lb{animation:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><label class="fl" for="cb">Country</label><div class="wr"><input id="cb" type="text" role="combobox" aria-expanded="false" aria-controls="lb" aria-autocomplete="list" autocomplete="off" placeholder="Start typing…"><ul id="lb" class="lb" role="listbox" aria-label="Countries" hidden></ul></div><p class="out" id="out" role="status"></p></div>
</div>
<script>
const C=['Algeria','Argentina','Australia','Brazil','Canada','Chile','Denmark','Egypt','France','Germany','Ghana','India','Italy','Japan','Kenya','Mexico','Nigeria','Norway','Peru','Spain'],cb=document.getElementById('cb'),lb=document.getElementById('lb'),out=document.getElementById('out');let list=[],act=0,last='';
const esc=s=>s.replace(/&/g,'&').replace(/</g,'<');
function hl(n,q){if(!q)return esc(n);const i=n.toLowerCase().indexOf(q.toLowerCase());return i<0?esc(n):esc(n.slice(0,i))+'<mark>'+esc(n.slice(i,i+q.length))+'</mark>'+esc(n.slice(i+q.length));}
function render(){const q=cb.value.trim().toLowerCase();list=C.filter(c=>c.toLowerCase().includes(q)).sort((a,b)=>(b.toLowerCase().startsWith(q)?1:0)-(a.toLowerCase().startsWith(q)?1:0));act=Math.min(act,Math.max(0,list.length-1));lb.hidden=false;cb.setAttribute('aria-expanded','true');
lb.innerHTML=list.length?list.map((c,i)=>'<li role="option" id="o'+i+'" data-i="'+i+'" aria-selected="'+(i===act)+'">'+hl(c,q)+'</li>').join(''):'<li class="no" role="option" aria-selected="false">No countries found</li>';if(list.length){cb.setAttribute('aria-activedescendant','o'+act);const el=document.getElementById('o'+act);if(el)el.scrollIntoView({block:'nearest'});}else cb.removeAttribute('aria-activedescendant');out.textContent=list.length+' result'+(list.length===1?'':'s')+'.';}
function close(){lb.hidden=true;cb.setAttribute('aria-expanded','false');cb.removeAttribute('aria-activedescendant');}
function pick(i){const c=list[i];if(!c)return;cb.value=c;last=c;close();out.textContent='Selected '+c+'.';}
cb.addEventListener('input',()=>{act=0;render();});cb.addEventListener('focus',render);
cb.addEventListener('keydown',e=>{if(e.key==='ArrowDown'){e.preventDefault();if(lb.hidden)render();else{act=(act+1)%Math.max(1,list.length);render();}}else if(e.key==='ArrowUp'&&!lb.hidden){e.preventDefault();act=(act-1+list.length)%Math.max(1,list.length);render();}else if(e.key==='Enter'&&!lb.hidden){e.preventDefault();pick(act);}else if(e.key==='Escape'&&!lb.hidden){e.preventDefault();cb.value=last;close();}});
lb.addEventListener('mousedown',e=>{const li=e.target.closest('li[data-i]');if(li){e.preventDefault();pick(+li.dataset.i);}});cb.addEventListener('blur',()=>setTimeout(()=>{close();if(cb.value&&!C.includes(cb.value))cb.value=last;},100));
</script>
</body></html>