Which browsers support datalist




















Since datalists have no built-in mechanism to require that a user select a provided option, they are well suited for inputs that can accept any value. The earlier example of providing a sports team fits here. While the datalist suggested teams, the user was free to input any value. Conversely, the U.

Such a use case is better handled by the select element because it forces a selection. One exception to this is inputs with a large number of valid selections. For example, consider the traditional country drop-down list shown in Figure 6. Figure 6. Standard Country Drop-Down. An autocomplete feature fits this use case well because a user can quickly filter the list by typing one or two characters.

Figure 7. While datalists do not natively allow you to require that an option be selected, you can easily add validation that does so. The constraint validation API is implemented in all browsers that support datalists, so if the datalist works, the validation should work as well. Now, when the user attempts to submit a form with an input that has a datalist and they have not selected an option , they will see the error shown in Figure 8.

Figure 8. Users working with older browsers will not have the constraint validation API available, and malicious users can easily subvert client-side scripts. While this approach works in modern browsers, it presents an unusable UI to users running browsers that lack support.

This can be accomplished by detecting whether the browser supports datalists and then polyfilling appropriately. Unlike with many new HTML5 features, for most use cases, no extra work needs to be done in browsers that do not support datalists. By default, the list of options you provide are merely suggestions; therefore, users with browsers that lack support will simply need to fill in the text field without any suggestions.

However, some fallback options can be used to provide a fuller experience to users running unsupporting browsers. One option, popularized by Jeremy Keith , is to take advantage of the fact that browsers that do not support the datalist element will still display child elements to the user.

The UI presented to users in browsers that support datalists will not change, but users working with browsers without support now see a select element with the country options and a text box they can use to enter any value. This is shown in Figure 9. The popular feature detection library Modernizr provides such a test, as shown here:.

Using this approach, you can now polyfill a datalist implementation for users in unsupporting browsers. The following code shows a polyfill implementation:.

Figure 10 shows the display of the country list example in Safari with the jQuery UI autocomplete polyfill. Figure The following example shows how you can build an autocomplete feature that matches options only at the beginning of the string:.

To make any polyfill of the datalist element work in older versions of Internet Explorer, you need to take two extra steps. The first is that Internet Explorer version 9 and earlier all require that option elements be immediate children of select elements. If they are not, these versions do not recognize them, and they will not be visible to the polyfill. Luckily, this is easy to work around.

By using conditional comments , you can place a select element around the options only for these versions of Internet Explorer. Refer to this example:. Internet Explorer version 9 and earlier will now detect the options appropriately. Internet Explorer 10 will not be affected since conditional comments were removed from the parser in Internet Explorer All other browsers will also ignore the comments.

In Internet Explorer version 8 and earlier, unknown elements cannot contain children. Although datalists are perfect for adding suggestions to text inputs, they suffer from a lack of customization capabilities that many modern Web applications require.

For example, you cannot do the following with datalists:. This means that if you need any of this functionality, you need to look into a more robust autocomplete solution.

The jQuery UI autocomplete widget provides the ability to do all of this and more. The autocomplete widget also supports all browsers back to Internet Explorer 7 without the need of a polyfill.

For more information on the autocomplete widget, check out its demos and API documentation. Datalists provide a quick, native means of displaying input suggestions to the user.

For situations when you do want to provide a functional datalist to all users, you can detect support and polyfill the functionality for browsers that lack support. While datalists are great for offering suggestions, they are limited in the functionality they provide.

How to create HTML forms. How to create a form in HTML and send it to email. I found your article very well written. Collectives on Stack Overflow. Learn more. Asked 6 years, 11 months ago. Active 2 years, 7 months ago. Viewed 46k times. Improve this question. DannyD DannyD 2, 15 15 gold badges 43 43 silver badges 63 63 bronze badges. Add a comment. Active Oldest Votes. Original post: You can use a select element inside the datalist , and duplicate the option tag values as readable text in these.

Improve this answer. Community Bot 1 1 1 silver badge. Dave Everitt Dave Everitt To clarify, Safari will show a select element in front of the input.

It will not have the same behavior as a datalist where you can either enter arbitrary input or choose from a list of suggestions.

So a part of the behavior is dead in a not replaceable way for all such users But all this is another story, just a hope for future development of the w3c html 6 standards Joe, I see the reference in the docs but can't seem to get it working in Safari Has anyone gotten it to work? Tim Dearborn Tim Dearborn 1, 7 7 silver badges 16 16 bronze badges.

I've only found a way to convert the datalist into a dropdown list. Not easily. One hack: you can create a select box with an "Other" option at the top or bottom.

When "Other" is selected, you could reveal a text box for the user to type in something not in the list. What about some kind of css and javascript that would emulate an autocomplete in safari? It looks like the code supports browsers that do not support datalists.

That should work very nicely. George George 2, 1 1 gold badge 21 21 silver badges 55 55 bronze badges. We went for this one to support datalist on safari. Thank you for your recommendation. It works perfect. I made some mods for aesthetic reasons.

Check it in link — tdolphin. I am having issues with safari Works great on iOS chrome — Peter Sun. It seems that the developer of the mdn recommended polyfill stays quite up to date: Update: Safari TP supports the datalist element at least basically, and its functionality will be included within the next release of Safari both for iOS and MacOS X.

Motine Motine 1, 13 13 silver badges 14 14 bronze badges. Eric Aya Mianto Mianto 19 1 1 bronze badge. Thanks for the polyfill tip, works great! EvilJordan EvilJordan 6 6 silver badges 16 16 bronze badges.

Sign up or log in Sign up using Google. Sign up using Facebook.



0コメント

  • 1000 / 1000