Paulund
2013-11-05 #html5

HTML5 Datalist

Most people use the jQuery library jQuery UI in their applications, this gives you access to a number of useful elements that you can use, these consist of the colour picker, sliders, accordions, dialog boxes, menus and progress bars. One of the most useful elements that is in the jQuery UI library is the autocomplete element. jQuery UI Autocomplete This works by first creating a Javascript object which you can populate with a number of options which will be searched when you enter text in the autocomplete textbox.

<script>
  $(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
</script>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags" />
</div>

There are lots of new features that come with HTML5 most of them use additional Javascript APIs, others replace existing functionality that we would use Javascript for such as form validation can now be replaced with the require attribute. The placeholder attribute is another feature that you used to have to use Javascript for. There is an element in HTML5 called Datalist that will allow you to create an autocomplete boxes without using any Javascript. First you create the datalist element which has an ID and a option list.


<datalist id="availableTags">
    <option value="ActionScript">
    <option value="AppleScript">
    <option value="Asp">
    <option value="BASIC">
    <option value="C">
    <option value="C++">
    <option value="Clojure">
    <option value="COBOL">
    <option value="ColdFusion">
    <option value="Erlang">
    <option value="Fortran">
    <option value="Groovy">
    <option value="Haskell">
    <option value="Java">
    <option value="JavaScript">
    <option value="Lisp">
    <option value="Perl">
    <option value="PHP">
    <option value="Python">
    <option value="Ruby">
    <option value="Scala">
    <option value="Scheme">
</datalist>

Now we can create a input type of list to use these options in the autocomplete.


<input name="availableTags" list="availableTags" />

Browser Support

The datalist element is current supported in Chrome, Firefox and Opera. View the current supported table to see exactly what browsers support it. Caniuse Datalist