Home

Awesome

datalist-polyfill

Financial Contributors on Open Collective MIT license npm bundle size Total downloads ~ Npmjs jsDelivr CDN downloads Codacy Badge GitHub Super-Linter CodeQL Known Vulnerabilities datalist-polyfill on Npmjs code style: prettier XO code style Conventional Commits Join the chat at https://gitter.im/datalist-polyfill/community PRs Welcome Open Source Love Contributor Covenant

Update: Safari now supports the datalist element at least basically, as announced earlier in 2019 with the latest release of Safari both for iOS and MacOS X. Yeah !!! Exciting news! I'm planning to release a new major version soon to both cheer as well as accommodate their implementation.

This is a minimal and dependency-free vanilla JavaScript polyfill for the awesome datalist-functionality, that will bring joy and happiness into our lives :-)

Features

Core concepts

The polyfill was designed with the following concepts kept in mind:

Installation

Just integrate the JavaScript file into your code - et voilà.

You may optionally load via NPM or Bower:

$ npm install datalist-polyfill
$ bower install datalist-polyfill

API

Nothing really, just plug it in, it will should work out of the box.

This package is also enabling the .options (for datalist elements) and .list (for input elements) properties according to the specs.

If you set a title-Attribute on the <datalist> HTML tag, it would get used as label for the first disabled entry within the polyfilling select on non-touch interactions.

dynamic HTML (or DHTML, if you like to be a little bit nostalgic)

In case that you'd like to dynamically add or modify / create your HTML code, you're good to go with this polyfill, as it's based on event delegation and additionally using MutationObserver (IE11+) that makes your UI work easily - no refresh nor reinit function to call after DOM manipulation or something similar.

Changes to the available option elements

If you'd like to make a change to the integrated list of <option> elements, feel free to either remove or add them right away - the list would get generated on the fly after the user typed in something into the <input> field, so you're covered on this.

You can also disable <option> elements by adding the disabled attribute to the <option> HTML tag if necessary.

Differing value and label values

As the browser vendors (Google Chrome vs. the others) don't seem to be aligned on this topic, I've decided to enable the label-attribute to serve as the definitive label being displayed, even if a value is being defined differing from the label. On different value and text values, both of them would get displayed within the suggestions, as Google Chrome does it. But if you define a differing label-attribute, its value would get displayed exclusively (as all the other browsers do it) to give you some flexibility on how to define those suggestions. Check out the „Different ways of defining an option“ section on the demo page regarding this topic.

value property on the option elements for Microsoft IE 10 & IE 11 and Edge

As explained in detail below in the section "Microsoft Internet Explorer 10 & 11 and Microsoft Edge", for fixing missing behaviour in IE 10+ and Edge we're manipulating the value for the option elements in those browser so you can't access them securely as a getter, but would need to take the original values out of data-originalvalue.

Microsoft Internet Explorer

Microsoft Edge

Microsoft Edge doesn't trigger the input event any more after selecting an item via mouseclick (on input elements other than type of text), even though that IE11 still did, nevermind ...

That for the optimizations on substring matching for Microsoft Edge specifically by #GH-39 (as explained further in the following "Microsoft Internet Explorer 10 & 11 and Microsoft Edge" section) need to get restricted to input[type="text"] elements even only.

There might be possible solutions to even also achieve the expected behaviour on non-text-input elements - even though that I only could think about ugly solutions that I don't want to have within the polyfill and that might even also break existing CSS & JS architecture / selectors.

Microsoft Internet Explorer 10 & 11 and Microsoft Edge

As mentioned with #GH-63, related to aspects reported via #GH-36 and #GH-39 (and in Microsoft Edges platform issues), it doesn't work in IE 10 & 11 as well as in Edge to "Search both the value and label, using substring matching; currently it searches both the value and label, but uses prefix matching".

As requested with #GH-36 we wanted to even also enrich the experience within the "newest" IE versions (10 & 11) and Edge browsers that provided basic support, but not the substring matching for users input. In this case the technical solution has been to manipulate the values in a way that the browser could actually handle that functionality as well, by including the users input at the beginning of the value after a substring matching to the original value, followed by a unique string for preventing any inconsistencies, followed by the original value itself, in this case for the sorting of the entries (this is mainly done in the updateIEOptions function around line 191 to 200 of the code).

This actually leads to a different behavior for the developers on the value property of each option elements within the datalist element for IE & Edge, but on the other hand provides a better UX for IE & Edge users by a consistent behavior for the user.

Microsoft Internet Explorer 9

You'll need the declaration for the standard hidden attribute, that you might already have included in case you're using normalize.css. Otherwise just adapt it from there:

/**
 * Add the correct
 * display in IE 10-
 */

[hidden] {
	display: none;
}

And you need to add a nesting select element wrapped by a conditional comment into the datalist element. Please have a look at the demo page accordingly, the code is being listed at the beginning.

Demo

See the polyfill in action either by downloading / forking this repo and have a look at demos/index.html and demos/ie9/index.html, or at the hosted demo: https://mfranzke.github.io/datalist-polyfill/demos/ and https://mfranzke.github.io/datalist-polyfill/demos/ie9/

things to keep in mind

Credits

Supported by Christian, Johannes, @mitchhentges, @mertenhanisch, @ailintom, @Kravimir, @mischah, @hryamzik, @ottoville, @IceCreamYou, @wlekin, @eddr, @beebee1987, @mricherzhagen, @acespace90, @damien-git, @nexces, @Sora2455, @jscho13, @alexirion and @vinyfc93. Thank you very much for that, highly appreciated !

Tested with

Big Thanks

Cross-browser testing platform provided by CrossBrowserTesting

CrossBrowserTesting

Prospects & functionality overview

The following problems are mainly reported and listed on caniuse as well as due to issues flagged on Github.

<table> <tr> <th>Problem</th> <th>IE9</th> <th>iOS</th> <th>Safari < 12.1</th> <th>iOS WebView</th> <th>Safari >= 12.1</th> <th>IE11+</th> <th>Edge</th> <th>Firefox</th> <th>Chrome</th> <th>Chrome WebView</th> </tr> <tr> <th align="left">Basic functionality</th> <td colspan="3" align="center">✔ <i>Polyfill</i></td> <td align="center">✔ via WKWebView</td> <td colspan="5" align="center">✔</td> <td align="center"><a href="https://github.com/mfranzke/datalist-polyfill/issues/33">#GH-33</a></td> </tr> <tr> <th align="left"><a href="https://bugs.chromium.org/p/chromium/issues/detail?id=773041">long lists of items are unscrollable resulting in unselectable options</a></th> <td colspan="8" align="center">✔</td> <td align="center"><a href="https://bugs.chromium.org/p/chromium/issues/detail?id=773041" target="_blank">fixed with v.69</a></td> <td align="center">✔</td> </tr> <tr> <th align="left"><a href="https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/9573654/">No substring matching for the suggestions</a></th> <td colspan="5" align="center">✔</td> <td colspan="2" align="center">✔ by <a href="https://github.com/mfranzke/datalist-polyfill/issues/39">#GH-39</a></td> <td colspan="3" align="center">✔</td> </tr> <tr> <th align="left"><a href="https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/20066595/">`datalist` popups gets &quot;emptied&quot; when receiving focus via tab</a></th> <td colspan="6" align="center">✔</td> <td align="center">✔ by <a href="https://github.com/mfranzke/datalist-polyfill/issues/49">#GH-49</a></td> <td colspan="3" align="center">✔</td> </tr> </table>

Outro

Personally I even also do like the "keep it simple" approach provided within the W3C specs even already.

But on the other hand this leads to an additional visible field, but doesn't emulate the (hopefully, fingers crossed) upcoming x-browser implementation and leaves unnecessary syntax for all of the clients that wouldn't even need it (anymore).

If you're trying out and using my work, feel free to contact me and give me any feedback. I'm curious about how it's gonna be used.

And if you do like this polyfill, please consider even also having a look at the other polyfill we've developed: https://github.com/mfranzke/loading-attribute-polyfill

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute]. <a href="https://github.com/mfranzke/datalist-polyfill/graphs/contributors"><img src="https://opencollective.com/datalist-polyfill/contributors.svg?width=890&button=false" /></a>

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

<a href="https://opencollective.com/datalist-polyfill"><img src="https://opencollective.com/datalist-polyfill/individuals.svg?width=890"></a>

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

<a href="https://opencollective.com/datalist-polyfill/organization/0/website"><img src="https://opencollective.com/datalist-polyfill/organization/0/avatar.svg"></a> <a href="https://opencollective.com/datalist-polyfill/organization/1/website"><img src="https://opencollective.com/datalist-polyfill/organization/1/avatar.svg"></a> <a href="https://opencollective.com/datalist-polyfill/organization/2/website"><img src="https://opencollective.com/datalist-polyfill/organization/2/avatar.svg"></a> <a href="https://opencollective.com/datalist-polyfill/organization/3/website"><img src="https://opencollective.com/datalist-polyfill/organization/3/avatar.svg"></a> <a href="https://opencollective.com/datalist-polyfill/organization/4/website"><img src="https://opencollective.com/datalist-polyfill/organization/4/avatar.svg"></a> <a href="https://opencollective.com/datalist-polyfill/organization/5/website"><img src="https://opencollective.com/datalist-polyfill/organization/5/avatar.svg"></a> <a href="https://opencollective.com/datalist-polyfill/organization/6/website"><img src="https://opencollective.com/datalist-polyfill/organization/6/avatar.svg"></a> <a href="https://opencollective.com/datalist-polyfill/organization/7/website"><img src="https://opencollective.com/datalist-polyfill/organization/7/avatar.svg"></a> <a href="https://opencollective.com/datalist-polyfill/organization/8/website"><img src="https://opencollective.com/datalist-polyfill/organization/8/avatar.svg"></a> <a href="https://opencollective.com/datalist-polyfill/organization/9/website"><img src="https://opencollective.com/datalist-polyfill/organization/9/avatar.svg"></a>