Submitting a form with datalist
Tags: datalist, inputs, forms, autosubmit
Published: 2020-08-26 19:53:00

---
title: Submitting a form with datalist
published: true
date: 2020-08-26 15:00:22 UTC
tags: datalist,inputs,forms,autosubmit
canonical_url: https://adactio.com/journal/17337
---

I’m a big fan of [HTML5’s `datalist` element](https://html5forwebdesigners.com/forms/#datalist) and [its elegant design](https://adactio.com/journal/4272/). It’s a way to progressively enhance any `input` element into [a combobox](https://en.wikipedia.org/wiki/Combo_box).

You use the `list` attribute on the `input` element to point to the ID of the associated `datalist` element.

```
<label for="homeworld">Your home planet</label>
<input type="text" name="homeworld" id="homeworld" list="planets">
<datalist id="planets">
 <option value="Mercury">
 <option value="Venus">
 <option value="Earth">
 <option value="Mars">
 <option value="Jupiter">
 <option value="Saturn">
 <option value="Uranus">
 <option value="Neptune">
</datalist>

```

[It even works on `input type="color"`](https://christianheilmann.com/2020/04/22/limiting-input-type-color-to-a-certain-palette-from-an-image/), which is pretty cool!

The most common use case is as an autocomplete widget. That’s how I’m using it over on [The Session](https://thesession.org), where the `datalist` is updated via Ajax every time the input is updated.

But let’s stick with a simple example, like the list of planets above. Suppose the user types “jup” …the datalist will show “Jupiter” as an option. The user can click on that option to automatically complete their input.

It would be handy if you could automatically submit the form when the user chooses a datalist option like this.

Well, tough luck.

The `datalist` element emits no events. There’s no way of telling if it has been clicked. This is something I’ve been trying to find a workaround for.

I got my hopes up when I read [Amber’s excellent article about `document.activeElement`](https://amberwilson.co.uk/blog/where-did-the-focus-go/). But no, the focus stays on the input when the user clicks on an option in a datalist.

So if I can’t _detect_ whether a datalist has been used, this best I can do is try to _infer_ it. I know it’s not exactly the same thing, and it won’t be as reliable as true detection, but here’s my logic:

- Keep track of the character count in the input element.
- Every time the input is updated in any way, check the current character count against the last character count.
- If the difference is greater than one, something interesting happened! Maybe the user pasted a value in …or maybe they used the datalist.
- Loop through each of the options in the datalist.
- If there’s an exact match with the current value of the input element, chances are the user chose that option from the datalist.
- So submit the form!

Here’s how that translates into DOM scripting code:

```
document.querySelectorAll('input[list]').forEach( function (formfield) {
  var datalist = document.getElementById(formfield.getAttribute('list'));
  var lastlength = formfield.value.length;
  var checkInputValue = function (inputValue) {
    if (inputValue.length - lastlength > 1) {
      datalist.querySelectorAll('option').forEach( function (item) {
        if (item.value === inputValue) {
          formfield.form.submit();
        }
      });
    }
    lastlength = inputValue.length;
  };
  formfield.addEventListener('input', function () {
    checkInputValue(this.value);
  }, false);
});

```

[I’ve made a gist](https://gist.github.com/adactio/2e3ec8cc3cc1c3b7305accb50144bf1b) with some added feature detection and mustard-cutting at the start. You should be able to drop it into just about any page that’s using datalist. It works even if the options in the datalist are dynamically updated, like the example on [The Session](https://thesession.org/).

It’s not foolproof. The inference relies on the difference between what was previously typed and what’s autocompleted to be more than one character. So in the planets example, if someone has type “Jupite” and then they choose “Jupiter” from the datalist, the form won’t automatically submit.

But still, I reckon it covers most common use cases. And like the `datalist` element itself, you can consider this functionality a progressive enhancement.