npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@aarongustafson/dynamic-datalist

v2.1.0

Published

Web component that enables you to dynamically update a field’s `datalist` with values retrieved from a URL as the user types

Readme

Dynamic datalist Web Component

npm version Build Status

Web component that enables you to dynamically update a field’s datalist with values retrieved from a URL as the user types

Demos

Installation

npm install @aarongustafson/dynamic-datalist

Usage

Option 1: Import the class and define manually

Import the class and register the element with your preferred tag name:

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

customElements.define('my-datalist', DynamicDatalistElement);

Option 2: Auto-define the custom element (browser environments only)

Use the guarded definition helper to register the element when customElements is available:

import '@aarongustafson/dynamic-datalist/define.js';

If you prefer to control when the element is registered, call the helper directly:

import { defineDynamicDatalist } from '@aarongustafson/dynamic-datalist/define.js';

defineDynamicDatalist();

You can also include the guarded script from HTML:

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

Basic Example (GET Request)

Wrap an input element with <dynamic-datalist> and specify an endpoint:

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

This will make a GET request to /api/search?query=WHAT_THE_USER_TYPED.

POST Request

<dynamic-datalist endpoint="/api/search" method="post">
  <input type="text" name="search" placeholder="Type to search..." />
</dynamic-datalist>

This will make a POST request with JSON body: { "query": "WHAT_THE_USER_TYPED" }.

Custom Variable Name

<dynamic-datalist endpoint="/api/search" key="term">
  <input type="text" name="search" placeholder="Type to search..." />
</dynamic-datalist>

This will send the query as /api/search?term=WHAT_THE_USER_TYPED (GET) or { "term": "..." } (POST).

Using Existing Datalist

If your input already has a datalist, the component will use and update it:

<dynamic-datalist endpoint="/api/cities">
  <input type="text" list="cities-list" placeholder="Type a city..." />
  <datalist id="cities-list">
    <option>New York</option>
    <option>Los Angeles</option>
    <option>Los Angeles</option>
    <option>Chicago</option>
  </datalist>
</dynamic-datalist>

The component will preserve and update the existing datalist instead of creating a new one.

API Response Format

Your endpoint should return JSON in this format:

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

Attributes

| Attribute | Type | Default | Description | |-----------|------|---------|-------------| | endpoint | string | (required) | URL to the JSON endpoint | | method | string | "get" | HTTP method: get or post | | key | string | "query" | Variable name for the query parameter |

Events

The component fires custom events that you can listen to:

| Event | Description | Detail | |-------|-------------|--------| | dynamic-datalist:ready | Fired when component is initialized | { input, datalist } | | dynamic-datalist:update | Fired when datalist is updated | { input, datalist, options } | | dynamic-datalist:error | Fired when an error occurs | { input, datalist, error } |

Example Event Handling

const element = document.querySelector('dynamic-datalist');

element.addEventListener('dynamic-datalist:update', (event) => {
  console.log('Received options:', event.detail.options);
});

element.addEventListener('dynamic-datalist:error', (event) => {
  console.error('Error fetching options:', event.detail.error);
});

Import Options

Auto-define (Recommended)

import '@aarongustafson/dynamic-datalist/define.js';
// Registers <dynamic-datalist> when customElements is available

You can also call the helper manually:

import { defineDynamicDatalist } from '@aarongustafson/dynamic-datalist/define.js';

defineDynamicDatalist();

Manual Registration

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

customElements.define('my-datalist', DynamicDatalistElement);

Browser Support

This component uses modern web standards:

  • Custom Elements v1
  • ES Modules
  • Fetch API
  • URLSearchParams

For older browsers, you may need polyfills.

Migration from jQuery Plugin

This component replaces my older jQuery plugin jquery.easy-predictive-typing.js.

Development

# Install dependencies
npm install

# Run tests
npm test

# Run tests with coverage
npm run test:coverage

# View demo
open demo/index.html

License

MIT © Aaron Gustafson