HTML 5 DataList Element with Fallback option for Old Browsers

A DropDown menu in HTML 5 is accomplished using a combination of the new DataList element and the list attribute. This is done in two steps:

Step 1: Create a datalist and populate it with option elements as shown below:

<datalist id=tvseries>
    <option value=Heroes>
    <option value=LOST>
    <option value=Fringe>
    <option value=CriminalMinds>
    <option value=CSIMiami>

Observe the ID attribute which is important to link this datalist with other elements on the page.

Step 2: Create an input element and link it to the datalist by setting the list attribute to the id of the associated datalist

<input type=text id=tv list=tvseries name=series autofocus>

When you run this example in a browser that support the HTML 5 datalist element and set focus on the TextBox, you will see a dropdown menu as follows:

HTML 5 DataList

The datalist element, as of this writing is supported only in FireFox 4+ and Opera 11.

In browsers like IE9, Chrome, Safari etc. that currently have not implemented support for the datalist element, you will see a regular text box. However this is not very consistent. What we would like is that in browsers that do not support DataList, atleast a standard dropdown control (SELECT element) should appear, which will allow users to select a value.

Note: Unlike the select element, users can enter data of their choice in datalist. It’s just that  they’ll be presented with a set of suggested options in a drop-down as they type. That’s what makes the datalist control exciting to add to your forms.

FallBack Option for Browsers not supporting HTML 5 DataList

Here’s how we can provide support for browsers not supporting the DataList by doing some minor changes in the code written previously

HTML 5 DataList

As you can observe, we have enclosed a ‘select’ element inside the datalist. All browsers supporting the datalist element will ignore the select element and display only the   HTML5 datalist element.

However all older browsers or ones without the datalist support will display a dropdown. Cool ain’t it!

Additionally, we have also added text for each of the option elements


This is done because the select element in old browsers does not display the value attribute, but instead displays the text specified.

That’s it. Run the page and see the output


Remember that the Select list will only be shown in browsers that do not currently support the datalist element like IE, Chrome, Safari.

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal


Anonymous said...

awesome, but it is w3c compatible, thanks

Anonymous said...

Ugliest code I've seen in a long time. Anyone that uses your example is polluting the internet with invalid and sloppy code. Keep googling, folks. Nothing to see here.

Suprotim Agarwal said...

thanks for your comment but then you need to also say what was ugly, invaid and sloppy about the code. The code shows one of the ways you provide a fallback option and its as simple as that!

Veniamin Ilmer said...

Here is an alternative way to provide compatibility as a "Suggestion":