惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
Simon Willison's Weblog
Simon Willison's Weblog
B
Blog
V
Visual Studio Blog
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
S
SegmentFault 最新的问题
博客园 - 司徒正美
博客园 - 【当耐特】
T
Tenable Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
宝玉的分享
宝玉的分享
N
Netflix TechBlog - Medium
S
Secure Thoughts
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
IT之家
IT之家
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
大猫的无限游戏
大猫的无限游戏
PCI Perspectives
PCI Perspectives
H
Hackread – Cybersecurity News, Data Breaches, AI and More
阮一峰的网络日志
阮一峰的网络日志
P
Privacy International News Feed
N
News and Events Feed by Topic
H
Hacker News: Front Page
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure
Google Online Security Blog
Google Online Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
H
Heimdal Security Blog
Project Zero
Project Zero
C
CERT Recently Published Vulnerability Notes
MyScale Blog
MyScale Blog
AI
AI
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
WordPress大学
WordPress大学
L
Lohrmann on Cybersecurity
TaoSecurity Blog
TaoSecurity Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CXSECURITY Database RSS Feed - CXSecurity.com
Spread Privacy
Spread Privacy
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
SecWiki News
SecWiki News
C
Cisco Blogs
The Last Watchdog
The Last Watchdog

Aaron Gustafson: Latest Posts

Can Your AI Pass the Accessibility Test? :: Aaron Gustafson Fixing Accessibility After the Fact Is Too Late :: Aaron Gustafson Easy Data-entry Verification with a Web Component :: Aaron Gustafson Visual Validation Feedback for Form Fields :: Aaron Gustafson Never Lose Form Progress Again :: Aaron Gustafson Accessibility Assistant for Figma v52 :: Aaron Gustafson Repeatable Form Fields Made Simple :: Aaron Gustafson A Production-Ready Web Component Starter Template :: Aaron Gustafson Fullscreen Video and Iframes Made Easy :: Aaron Gustafson Lazy Loading Images Based on Screen Size :: Aaron Gustafson A Web Component for Obfuscating Form Fields :: Aaron Gustafson Optimizing Your Codebase for AI Coding Agents :: Aaron Gustafson A Web Component for Conditionally Displaying Fields :: Aaron Gustafson Identifying Accessibility Data Gaps in CodeGen Models :: Aaron Gustafson Learning Web Design, 6th Edition is out! :: Aaron Gustafson Passing Your CSS Theme to `canvas` :: Aaron Gustafson Exploring AI’s Role in Accessibility :: Aaron Gustafson Complaining About Designers Fiddling with Figma Solves Nothing :: Aaron Gustafson On Diversity :: Aaron Gustafson A Web Component for Conditional Dependent Fields :: Aaron Gustafson On CrowdStrike, dependencies, and building robust products on the web :: Aaron Gustafson Requirement Rules for Checkboxes :: Aaron Gustafson Don’t Outsource Your Perspective to a LLM :: Aaron Gustafson One World, One Web, One Love :: Aaron Gustafson
Dynamic Datalist: Autocomplete from an API :: Aaron Gustafson
Aaron Gustafson · 2025-12-16 · via Aaron Gustafson: Latest Posts

HTML’s datalist element provides native autocomplete functionality, but it’s entirely static—you have to know all the options up front. The dynamic-datalist web component solves this by fetching suggestions from an API endpoint as users type, giving you the benefits of native autocomplete with the flexibility of dynamic data.

This component is a modern replacement for my old jQuery predictive typing plugin. I’ve reimagined it as a standards-based web component.

Basic usage

To use the component, wrap it around your input field and specify an endpoint:

<dynamic-datalist endpoint="/api/search">
  <label for="search"
    >Search
    <input
      type="text"
      id="search"
      name="search"
      placeholder="Type to search..."
    />
  </label>
</dynamic-datalist>

As users type, the component makes GET requests to that endpoint, passing in the typed value as the “query” parameter (e.g., /api/search?query=WHAT_THE_USER_TYPED). The response from the endpoint is used to populates a dynamic datalist element with the results.

The structure of the response should be JSON with an options array of string values:

{
  "options": ["option 1", "option 2", "option 3"]
}

How it works

Under the hood, the component:

  1. Adopts (or creates) a datalist element for your input,
  2. Listens for “input” events,
  3. Debounces requests (waiting at least 250ms) to avoid overwhelming your API,
  4. Sends requests to your endpoint with the current value of the input,
  5. Reads back the JSON response,
  6. Updates the datalist option elements, and
  7. Dispatches the update event.

All of this happens transparently—users just see autocomplete suggestions appearing as they type.

Need POST?

You can change the submission method via the method attribute:

<dynamic-datalist endpoint="/api/lookup" method="post">
  <label for="lookup"
    >Lookup
    <input type="text" id="lookup" name="lookup" />
  </label>
</dynamic-datalist>

This sends a POST request with a JSON body: { "query": "..." }. Currently GET and POST are supported, but I could add more if folks want them.

Custom variable names

As I mentioned, the component uses “query” as the parameter name by default, but you can easily change it via the key attribute:

<dynamic-datalist endpoint="/api/terms" key="term">
  <label for="search"
    >Term search
    <input type="text" id="search" name="term" />
  </label>
</dynamic-datalist>

This sends the GET request /api/terms?term=....

Working with existing datalists

If your input already has a datalist defined, the component will inherit it and replace the existing options with the fetched results, which makes for a nice progressive enhancement:

<dynamic-datalist endpoint="/api/cities">
  <label for="city"
    >City
    <input
      type="text"
      id="city"
      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>

Users see the pre-populated cities immediately, and as they type, API results supplement the list. If JavaScript fails or the web component doesn’t load, users still get the static options. Nothing breaks.

Event handling

If you want to tap into the component’s event system, it fires three custom events:

  • dynamic-datalist:ready - Fired when the component initializes
  • dynamic-datalist:update - Fired when the datalist is updated with new options
  • dynamic-datalist:error - Fired when an error occurs fetching data
const element = document.querySelector("dynamic-datalist");

element.addEventListener("dynamic-datalist:ready", (e) => {
  console.log("Component ready:", e.detail);
});

element.addEventListener("dynamic-datalist:update", (e) => {
  console.log("Options updated:", e.detail.options);
});

element.addEventListener("dynamic-datalist:error", (e) => {
  console.error("Error:", e.detail.error);
});

Each event provides helpful detail objects with references to the input, datalist, and other relevant data.

Demo

Check out the demo for live examples (there are also unpkg and ESM builds if you want to test CDN delivery):

Grab it

The project is available on GitHub. You can also install via npm:

npm install @aarongustafson/dynamic-datalist

If you go that route, there are a few ways to register the element depending on your build setup:

Option 1: Define it yourself

import { DynamicDatalistElement } from "@aarongustafson/dynamic-datalist";

customElements.define("dynamic-datalist", DynamicDatalistElement);

Option 2: Let the helper guard registration

import "@aarongustafson/dynamic-datalist/define.js";
// or, when you need to wait:
import { defineDynamicDatalist } from "@aarongustafson/dynamic-datalist/define.js";

defineDynamicDatalist();

Option 3: Drop the helper in via a <script> tag

<script
  src="./node_modules/@aarongustafson/dynamic-datalist/define.js"
  type="module"
></script>

Regardless of how you register it, there are no framework dependencies—just clean autocomplete powered by your API. As I mentioned, it’s also available via CDNs, such as unpkg too, if you’d prefer to go that route.