The left shows simple datalist. The right shows the "display country name, submit 2-letter code" pattern using a hidden input. Watch the badge and log.
There is no native selection event on datalist. This demo uses the input event to compare the current value against all options in real time. The status box shows which case applies.
Fetches from a public REST API (jsonplaceholder) as you type. Waits 300ms after your last keystroke. Cancels in-flight requests on new input. Won't fetch until you type 2+ characters.
datalist works with non-text input types too. Range gets tick marks, color gets preset swatches (Chrome/Edge), time gets quick-pick suggestions. Browser support varies — test on your targets.
By default datalist allows free text. Combine pattern + setCustomValidity() to enforce only listed values. The :user-invalid border and CSS error message appear on incorrect input.
Pick multiple options from a datalist. Each selection becomes a removable tag. The value is synced to a hidden input for form submission. Press Backspace to remove the last tag.
—