Autocomplete Input
Type-ahead search with remote data fetching and dropdown results
Built-in Features
What you get out of the box with zero configuration
Initializes automatically on any input with autocomplete="on" inside a container with data-url. For dynamic content, call NDS.Autocomplete.reinit(). The instance itself is built on the field's first focus, which covers every path a user takes but not a field that JS drives before anyone touches it: to write a value and fetch against a field that has never been focused, construct it first with NDS.Autocomplete.create(container).
Matching characters in results are highlighted with <mark> tags. Results display in a dropdown built on the NDS Dropmenu component.
Arrow keys navigate results, Enter selects the active item, Escape closes the dropdown, Tab closes without selecting, Home jumps to the first item, End jumps to the last item. The active item scrolls into view automatically.
API requests are debounced at 300ms. Previous in-flight requests are cancelled via AbortController. Loading state shows on the input during fetch.
Works with Arabic and English content. Empty state message adapts to the page language. RTL and LTR layouts supported.
Selected values sync to the input. Clear button resets the selection. Works with the forms validation and status API.
When the container also has class nds-search-box, selecting a result automatically clicks the nearest .nds-search-btn to submit the search without extra interaction.
Usage Guidelines
When and how to use autocomplete inputs effectively
When to Use
- Search fields that query a remote API for suggestions as the user types
- Large datasets where showing all options in a select dropdown is impractical
- Service search, city lookup, product search, or any entity search
- Set
data-min-charsto control when fetching begins (default: 3 characters) - For static option lists, use a select dropdown instead
JavaScript API
Configuration Attributes
data-urlon the container: API endpoint that returns JSONdata-name: JSON field name to display in results (default: "Title")data-min-chars: minimum characters before fetching starts (default: 3)data-query-param: query string parameter name sent to the API (default: "q")data-results-path: dot notation path to the results array in the response (e.g. "response.items"). Without it, the component auto-detects flat arrays or objects withresultsordatakeysdata-fetch: fetch mode, either"each"(default) or"once". With"each"the API is called on every keystroke and the server filters results. With"once"the full list is fetched once on first input, cached, and filtered client-side on each keystroke. Use"once"for small static datasets such as countries, currencies, or departments.data-empty-message: custom text for the "no results" placeholder shown when a query matches nothing (default: localized "No results")data-empty-icon: icon classes for the "no results" placeholder (default:nds-icon nds-hgi-search-01)
API Response Format
With the default data-fetch="each" mode, the component sends a GET request per keystroke (e.g. /api/services?q=term) and the server handles filtering. With data-fetch="once", the full URL is fetched once with no query parameter and filtering is done client-side. Both modes expect JSON in one of these formats: