Basic GET Request
Wrap a labeled input in
<dynamic-datalist> and set
endpoint to your API’s URL.
Using endpoint="/api/search" makes a GET
request to
/api/search?query=WHAT_THE_USER_TYPED.
<dynamic-datalist
endpoint="/api/search">
<label for="search1">Search
<input
type="text"
id="search1"
name="search"
placeholder="Type to search..." />
</label>
</dynamic-datalist>
Bootstrap Options
Populate the datalist when the component loads, without waiting for user input.
Adding bootstrap requests
/api/bootstrap without a query.
<dynamic-datalist
endpoint="/api/bootstrap"
bootstrap>
<label for="bootstrap-search">Search
<input
type="text"
id="bootstrap-search"
name="bootstrap-search"
placeholder="Choose or type..." />
</label>
</dynamic-datalist>
POST Request
Send the search query using a POST request with a JSON body.
Using method="post" makes a POST request
with JSON body: { "query": "..." }.
<dynamic-datalist
endpoint="/api/lookup"
method="post">
<label for="search2">Lookup
<input
type="text"
id="search2"
name="lookup"
placeholder="Type to lookup..." />
</label>
</dynamic-datalist>
Custom Variable Name
Change the variable name used for the query parameter.
Using key="term" sends the query as
/api/search?term=... (GET) or
{ "term": "..." } (POST).
<dynamic-datalist
endpoint="/api/terms"
method="post"
key="term">
<label for="search3">Term search
<input
type="text"
id="search3"
name="term"
placeholder="Type a term..." />
</label>
</dynamic-datalist>
Using Existing Datalist
Enhance existing datalist elements with dynamic fetching.
When the input already has a datalist, the component will reuse that list and replace its options after a successful response. Initial options remain available without JavaScript or before the first successful response. A failed request keeps the current options.
<dynamic-datalist
endpoint="/api/cities">
<label for="search4">City
<input
type="text"
id="search4"
list="cities-list"
placeholder="Type a city..." />
</label>
<datalist id="cities-list">
<option>New York</option>
<option>Los Angeles</option>
<option>Chicago</option>
</datalist>
</dynamic-datalist>
Real-World Examples
Common patterns and use cases for dynamic datalists.
Product Search
A typical e-commerce product search with autocomplete suggestions.
<form action="/search" method="get">
<dynamic-datalist
endpoint="/api/products">
<label for="product-search">
Search products
<input
type="search"
id="product-search"
name="q"
placeholder="Search for products..." />
</label>
</dynamic-datalist>
<button type="submit">Search</button>
</form>
User Mention/Tag
Fetch username suggestions as you type. The mock API
accepts a name with or without a leading @;
the component doesn’t wait for a special character.
<form action="/comment" method="post">
<dynamic-datalist
endpoint="/api/users"
key="username">
<label for="mention">
Mention a user
<input
type="text"
id="mention"
name="mention"
placeholder="Start typing @..." />
</label>
</dynamic-datalist>
<button type="submit">Post</button>
</form>
Address Autocomplete
Address lookup with autocomplete, useful for shipping or location forms.
<form action="/checkout" method="post">
<label for="name">Name
<input
type="text"
id="name"
name="name"
required />
</label>
<dynamic-datalist
endpoint="/api/addresses">
<label for="address">Address
<input
type="text"
id="address"
name="address"
placeholder="Start typing your address..."
required />
</label>
</dynamic-datalist>
<button type="submit">Continue</button>
</form>
Event Handling
Listen for initialization, updated suggestions, and request errors.
The component fires three custom events:
dynamic-datalist:ready,
dynamic-datalist:update, and
dynamic-datalist:error. This demo logs all
events in the visible log below the field.
<dynamic-datalist
id="example8"
endpoint="/api/search">
<label for="search8">Search with event logging
<input
type="text"
id="search8"
name="search"
placeholder="Type to trigger events..." />
</label>
</dynamic-datalist>
<div id="log8" class="log">
Ready: Component initialized
</div>
<script>
const element = document.getElementById('example8');
const log = document.getElementById('log8');
element.addEventListener(
'dynamic-datalist:ready',
() => {
log.textContent = 'Ready: Component initialized';
log.className = 'log';
});
element.addEventListener(
'dynamic-datalist:update',
(event) => {
const options = event.detail.options;
if (!Array.isArray(options)) return;
log.textContent = `Updated: Received ${options.length} option${options.length !== 1 ? 's' : ''}: ${options.slice(0, 3).join(', ')}${options.length > 3 ? ', ...' : ''}`;
log.className = 'log';
});
element.addEventListener(
'dynamic-datalist:error',
(event) => {
log.textContent = `Error: ${event.detail.error.message}. You can still type a value; existing suggestions remain available.`;
log.className = 'log error';
});
</script>
When suggestions can’t load
This endpoint deliberately returns an error. Type to try it. Your text and the initial suggestion remain available; you don’t have to choose a suggestion to continue.
<dynamic-datalist endpoint="/api/unavailable">
<label for="offline-search">Destination</label>
<input id="offline-search" list="offline-options" />
<datalist id="offline-options">
<option>Home</option>
</datalist>
</dynamic-datalist>
API Reference
For complete documentation of attributes, events, response format, and browser support, see the README.