The <datalist> element, a new addition in the HTML5 specification, allows developers to create native autocomplete dropdowns for their web applications. This type of user interface component is commonly used on form fields and search boxes as it helps the user input data faster. In the past, autocomplete dropdowns could only be achieved using JavaScript.

In this blog post, you’re going to learn how to use the <datalist> element to create native autocomplete dropdowns for your applications.

Let’s get started.

The <datalist> Element

A datalist autocomplete dropdown in Google Chrome.

The <datalist> element is used to specify all of the possible values for the autocomplete list. Each of these values is defined using an <option> element, as shown in the example below.

The <datalist> element should have an id attribute. To link your datalist to an <input> element, you need to specify a list attribute on the input and set its value to the id of your datalist.

<input type="text" list="languages">

That is all you need to add auto-complete functionality to an <input> element. The browser will take care of searching the datalist options for matching values and then displaying those to the user.

Loading Options via AJAX

A datalist autocomplete dropdown with options loaded via AJAX.

If you want to add a large number of options to your <datalist>, you may want to store those values in an external JSON file. You can then fetch this file via AJAX and populate the <datalist> options once the page has loaded. Let’s look at an example of how to do this.

Like before, we start with <input> and <datalist> elements. Ensure that you link the two using the id and list attributes.

Next, we need to load the JSON file (html-elements.json) and populate the options for the <datalist> element. For this example, we’re just using an array of strings, but it’s also possible to use a more complex data structure.

This code will load the html-elements.json file using an XMLHttpRequest. Once the file has been fetched, we parse the raw JSON data so that we have a JavaScript array to work with. We then loop over each of the items in this array. Each time we create a new <option> element, set the value attribute to the current item in the array, and then add this <option> element to the <datalist>.

In this example we’re also using the <input> element’s placeholder property to display the loading status for the datalist options.

Browser Support

Support for the <datalist> element among browsers is pretty good. Safari is the only modern web browser not to include support for <datalist>. It’s worth noting that the implementation of this element in IE10 is known to be buggy.

Final Thoughts

In this post, you’ve learned how to use the <datalist> element to create native autocomplete dropdowns for your web applications. You’ve also learned how to use AJAX to populate datalist options from an external JSON file.

The <datalist> element is just one of a number of new elements that provide native implementations of commonly used UI components. By making these elements really simple to use, browser vendors are removing the need for developers to rely on JavaScript libraries in these areas. Standardizing the behavior of UI components also has the added benefit of creating a more consistent experience for users across the web.

Useful Links

If you’re looking to take your programming skills to another level, check out our Techdegrees. Our faculty of tech professionals guide learners like you from mastering the fundamentals of coding to polishing the skills of a job-ready software developer. Try one of them out with a free seven-day trial today.

Hi, im working on a school project and I wanna know if this code will work if used in hybrid mobile development using cBordova? Because I’ve tried many source codes which will autocomplete a dropdown datalist but it only work when im using a browser and not when i integrate it with my code. I’m new to all of these so please bare with me. thanks!

I want the value of datalist to be autopopulated from SQL based on text entered in text box.

Suppose I enter ‘Banga’ automatically Bangalore shud be fetched from DB and i should be able to select that into text box.
Text box and datalist should be one field only not 2. How do i do this please help me. I am new to JSP and i am unable to find help on this.

Hello,
Do we option to select more then one input from the drop down menu?
There is a way to do it by “select multiple” attribute in HTML5 and we can do it through js too.
But I want to select the same option more then one time.
Please let me know if there is any alternative.

jewellery sets for rent in chennai|artificial bridal jewellery for rent in chennai|bridal jewellery sets in chennai for rent|bridal jewellery for rent in chennai|kundan bridal jewellery sets for rent in chennai|bridal jewellery set for rent in chennai|bri

Thank you, I’ve recently been searching for info approximately this topic for a long time and yours is the best I have found out till now. However, what about the bottom line? Are you positive concerning the source?|

It would be interesting if you could gather screenshots across a range of mobile devices to see how their ‘adaptive’ interfaces handle this (E.G. when a user interacts with a number field they’re displayed a keyboard with only numbers).

Each is designed by our faculty of tech professionals to guide even a coding beginner to becoming a job-ready software developer armed with a professional portfolio of real-world projects. Try one out today with our free seven-day trial, and see if software development is for you.