Have you ever had a form that needed to accept a short, arbitrary bit of text? Like a name or whatever. That’s exactly what
<input type="text"> is for. There are lots of different input types (and modes!), and picking the right one is a great idea.
But this little story is about something else and applies to any of them.
What if the text needs to be arbitrary (like “What’s your favorite color?”) so people can type in whatever, but you also want to be helpful. Perhaps there are a handful of really popular answers. Wouldn’t it be nice if people could just select one? Not a
<select>, but a hybrid between an input and a dropdown. Hold on though. Don’t make your own custom React element just yet.
<datalist> is for. I just used it successfully the other day so I figured I’d blog it because blogging is cool.
Here are the basics:
The use case I was dealing with needed:
<input type="text">for a username
<input type="text">for a “flag” (an aribtrary string representing a permission)
I probably wouldn’t do a
<datalist> for every username in a database. I don’t think there is a limit, but this is sitting in your HTML, so I’d say it works best at maybe 100 options or less.
But for that second one, we only had maybe 3-4 unique flags we were dealing with at the time, so a datalist for those made perfect sense. You can type in whatever you want, but this UI helps you select the most common choices. So dang useful. Maybe this could be useful for something like a gender input, where there is a list of options you can choose, but it doesn’t enforce you actually choose one of them.
Even lesser known than the fact that
<datalist> exists? The fact that it works for all sorts of inputs besides just
range, and even
The post Datalist is for suggesting values without enforcing values appeared first on CSS-Tricks.