I’m a big fan ofHTML5’sdatalist
element andits elegant design. It’s a way to progressively enhance anyinput
element intoa combobox.
You use thelist
attribute on theinput
element to point to the ID of the associateddatalist
element.
<label for="homeworld">Your home planet</label><input type="text" name="homeworld" list="planets"><datalist> <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 oninput type="color"
, which is pretty cool!
The most common use case is as an autocomplete widget. That’s how I’m using it over onThe Session, where thedatalist
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.
Thedatalist
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 readAmber’s excellent article aboutdocument.activeElement
. But no, the focus stays on the input when the user clicks on an option in a datalist.
So if I can’tdetect whether a datalist has been used, this best I can do is try toinfer 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 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 onThe Session.
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 thedatalist
element itself, you can consider this functionality a progressive enhancement.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse