Combobox
A text field with suggestions. Point any input at a <datalist> and it gets a Momo suggestion list instead of the browser's: typing filters it, best matches first, and capitals and accents don't matter.
Suggestions
Anything can be typed; the list only suggests. A tap on the field shows everything. Add data-detail for a second line and data-group for headings.
<div class="form-field w-72">
<label class="form-label" for="cb-city">City</label>
<input class="form-control" id="cb-city" name="city" list="cb-cities" placeholder="Try "sao"">
<datalist id="cb-cities">
<option value="Lisbon" data-detail="Portugal">
<option value="Porto" data-detail="Portugal">
<option value="São Paulo" data-detail="Brazil">
<option value="Rio de Janeiro" data-detail="Brazil">
<option value="Montréal" data-detail="Canada">
<option value="Toronto" data-detail="Canada">
<option value="Vancouver" data-detail="Canada">
<option value="Mexico City" data-detail="Mexico">
<option value="Seoul" data-detail="South Korea">
<option value="Tokyo" data-detail="Japan">
<option value="Osaka" data-detail="Japan">
<option value="Berlin" data-detail="Germany">
</datalist>
</div>- Keys: ↓ / ↑ open the list and move, Enter picks, Esc closes it (press again to clear the field).
- Without JavaScript it is still a normal input with the browser's own suggestions.
data-momo-nativeon the input keeps the browser's list.
Only values from the list
data-strict: the first match is highlighted so Enter or Tab takes it, capitals are fixed ("ana kim" becomes "Ana Kim"), and anything else makes the field invalid. data-id-field also fills a hidden input with the option's data-id, so the server gets an id, not a name. data-color adds a dot, data-icon a Moey icon.
<form class="form-field w-72" id="cb-assign" onsubmit="event.preventDefault();Momo.toast({ title: 'Assigned', text: 'id ' + this.assignee_id.value, color: 'green' })">
<label class="form-label" for="cb-assignee">Assign to</label>
<input class="form-control" id="cb-assignee" list="cb-team" data-strict data-id-field="#cb-assignee-id" required>
<input type="hidden" name="assignee_id" id="cb-assignee-id">
<datalist id="cb-team">
<option value="Ana Kim" data-detail="Design" data-color="pink" data-id="u-101">
<option value="Max Ruiz" data-detail="Support" data-color="cyan" data-id="u-102">
<option value="Lea Novak" data-detail="Engineering" data-color="lime" data-id="u-103">
<option value="Sam Okafor" data-detail="Sales" data-color="yellow" data-id="u-104">
</datalist>
<p class="form-hint">Pick someone from the team.</p>
<div><button class="btn btn-brand mt-2" type="submit">Assign</button></div>
</form>Add new values
data-create offers Add "..." when nothing matches. It fires momo:create: cancel it to say no, or save the value to your server. The value is added to the list.
<div class="form-field w-72">
<label class="form-label" for="cb-cat">Product category</label>
<input class="form-control" id="cb-cat" list="cb-cats" data-create>
<datalist id="cb-cats">
<option value="Mugs" data-group="Kitchen" data-icon="coffee">
<option value="Plates" data-group="Kitchen" data-icon="utensils">
<option value="Totes" data-group="Bags" data-icon="shopping-bag">
<option value="Backpacks" data-group="Bags" data-icon="backpack">
<option value="Posters" data-group="Prints" data-icon="image">
</datalist>
</div>input.addEventListener('momo:create', (e) => {
saveCategory(e.detail.value); // or e.preventDefault() to refuse
});From your server
data-source loads suggestions as people type; {q} is replaced with what was typed. Return a JSON array of strings, or of { value, label, detail, group, color, icon, id }. Requests wait until typing pauses (data-delay, default 200 ms), and an older request is cancelled when a newer one starts. data-min-chars sets how many letters it waits for (default 1).
<input class="form-control" data-momo-combobox data-source="/api/countries?q={q}">Or hand it a function:
Loaded by a function, with a fake half-second wait.
<div class="form-field w-72">
<label class="form-label" for="cb-customer">Customer</label>
<input class="form-control" id="cb-customer" type="search" data-momo-combobox>
<p class="form-hint">Loaded by a function, with a fake half-second wait.</p>
</div>
<script>
addEventListener('DOMContentLoaded', () => Momo.combobox('#cb-customer', {
source: async (query, signal) => {
await new Promise((r) => setTimeout(r, 500));
const people = [
{ name: 'Avery Chen', email: 'avery@example.com', id: 7 },
{ name: 'Bruno Silva', email: 'bruno@example.com', id: 8 },
{ name: 'Chloé Martin', email: 'chloe@example.com', id: 9 },
{ name: 'Dara Singh', email: 'dara@example.com', id: 10 },
];
const q = query.toLowerCase();
return people.filter((p) => p.name.toLowerCase().includes(q) || p.email.includes(q))
.map((p) => ({ value: p.name, detail: p.email, id: p.id }));
},
}));
</script>JavaScript
Momo.combobox('#customer', { source: async (query, signal) => [...] });
Momo.combobox.refresh('#city'); // read the datalist again after you change it
input.addEventListener('momo:pick', (e) => console.log(e.detail.value, e.detail.id));The field fires input and change when a suggestion is picked. Screen readers hear the field as a combobox, the highlighted suggestion, and how many there are.
Need a fixed list with no typing? Use the Select. Several values? Use the Tag input.