Datalist, the pure HTML typeahead you didn’t know about

If you ever want to build a searchable down payment field, <datalist> Can do it for you. In all browsers. Across devices. Complies with standards. Without JavaScript. 🤯

I learned about <datalist> Candidate for interview. It was great.

Let’s dig.

Searchable drop-down menu items are a combination of <input> Field and list of options. Typing filters the options so they are easier to find.

You get vague search, keyboard navigation, mouse navigation and Enter to select. A drop-down menu appears in focus, hiding in blur. The value selected is textual.

I got the list of dog breeds from the API.

An iPad with a connected keyboard shows it:

Datalist on iPad

And it even works on the phone 🤩

Data list on the phone

You can Try it for yourself in CodeSandbox

You start with an input field:

<input name="dogBreed" list="breeds" />

God list A feature defines which data list you want to use. This is a unique global identifier on the page.

Add a data list element with your options:

<datalist id="breeds">

<option value="affenpinscher"></option>

<option value="african"></option>

<option value="airedale"></option>

<option value="akita"></option>

<option value="appenzeller"></option>

<option value="australian shepherd"></option>

<option value="basenji"></option>


and that’s it. God id Recognizes your list and the browser handles the rest. You now have a full pre-field compliant field that works in any browser.

If you want to pack it as a reusable component, you will need to create the id and list Processing features. Should be unique on the page.

React data list should be used for reuse

  1. Work as multiple instances per page
  2. Rendering options from a list
  3. Return values ​​with reconnection

No. 3 is optional. You can do this using form machines like react-hook-form.

You end up with such a component:

const Datalist = ( name, options, onChange ) =>

const id = new Date().getTime()

return (


<input name=name list=id onChange=onChange />

<datalist id=id> => (

<option value=o />





We use a unique ID of a poor person – the current time in milliseconds. its Okay, But you will want to use useId When React 18 comes out and finds a stronger solution by then.

We read onChange For each input change. This can be annoying for our component consumers.

A nice option would be to check the value against your list of options at each change and call an onSelected Repeat when there is a match. This way you will catch when the user makes an explicit choice. Probably.

<datalist> As a Dynamic React component

If you have too many listing options or you need richer search behavior, you can expand this component with dynamic search. So:

const DynamicDatalist = ( name, api, onChange ) =>

const id = new Date().getTime()

const [options, setOptions] = useState([])

async function runSearch(event)

const value = event.currentTarget.value

const data = await fetchFrom(`$api?search=$value`)


return (


<input name=name list=id onChange=runSearch />

<datalist id=id> => (

<option value=o />





Suppose it is a pseudocode.

You watch changes, run API requests and use those to populate the list of options dynamically. Beware of running requests blindly at every tap.

Avatar by Eric BorelEric Burrell@ericbureltech

@Swizec I tried it here:. It seems to really work as expected! Thanks for the tip, just yesterday I was looking for a vanilla HTML key.

You can not style data list. I think it’s definitely better.

You also can not use it to build the popular combo thing popular these days. The input that displays your selections as bullets next to your typing.

Multiselect integrated, not possible with data list

Multiselect integrated, not possible with data list

You can not use rich option values ​​like objects. It’s a text. Unless you build it yourself as part of <Datalist> component.

And you will need to perform your own verification that the final value is on the list. normal <select> Maybe a better option there.

Next time you build a writing head, consider the <datalist>. It may serve you better than a 81kb React Library.

for life,
~ Swizec

post Scriptum: <datalist> Works with Vue, Svelte etc. I used React because it’s more convenient for me

Did you enjoy this article?

Posted in December 14, 2021 B Front, HTML

Here’s how it works 👇

Leave your email and I will send you carefully written emails about every week to respond, JavaScript, And Your career. The lessons learned over 20 years in the industry working with companies, from tiny startups to masses of Fortune5.

Join the Swizec newsletter

And receive considerate letters 💌 onwards Thinking, tactics and technical skills For your career. Real lessons learned from building production software. No nonsense.

“Man, love your simple writing! Yours is the only newsletter I open and the only blog I fuck to read and scroll to the end. Wow always take lessons with me. Inspirational! And very connected. 👌”

~ Ashish Kumar

Do you have a burning question that you think I can answer? I do not have all the answers, but I have some! Beat me Twitter Or Book a 30 min For in-depth help.

Ready to stop copying and pasting D3 samples and create your own data simulations?
Learn how to build extensible dataaviz components that your entire team can understand with Respond to data simulation

Curious about Serverless and the modern backend? to check
A directory without a server, Modern rear for front engineer.

Ready to learn how everything fits in and build a modern web app from scratch?
Learn how to launch a web app and make your first בצד side with ServerlessReact.Dev

Want to refresh your modern JavaScript syntax? Check out my interactive cheats page:

By the way, in case no one has told you this yet today: I love and appreciate you for who you are ❤️




Please enter your comment!
Please enter your name here