DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Quick Tip – Autocomplete Using HTML5 Datalist Element

Quick Tip – Autocomplete Using HTML5 Datalist Element

Gil Fink user avatar by
Gil Fink
·
Aug. 02, 12 · Interview
Like (0)
Save
Tweet
Share
11.38K Views

Join the DZone community and get the full member experience.

Join For Free
one of the new elements in html5 specifications is the datalist . the datalist holds a predefined options list which will be presented as autocomplete list in other html5 controls such as textboxes.

using datalist element

the use of a datalist element is straight forward. you create the datalist inside a phrasing element and write the autocomplete options inside of it:

<datalist id="dlcities">

    <option value="seattle" />

    <option value="las vegas" />

    <option value="new york" />

    <option value="salt lake city" />

</datalist>

in order to connect the previous list to a textbox and create the autocomplete functionality, you will use the new list attribute and give it the datalist id:

<input type="text" id="txt" name="txt" list="dlcities" />

the result of running the page will be:

datalist

creating on the fly datalist

one of the requirements i had in a project i did lately was to create a datalist on the fly. this can be achieved using basic javascript functionality. here the code sample:

(function () {

    var optionlist = ["seattle", "las vegas", "new york", "salt lake city"];

 

    function filldatalist() {

        var container = document.getelementbyid('container'),

        i = 0,

        len = optionlist.length,

        dl = document.createelement('datalist');

 

        dl.id = 'dlcities';

        for (; i < len; i += 1) {

            var option = document.createelement('option');

            option.value = optionlist[i];

            dl.appendchild(option);

        }

        container.appendchild(dl);

    }

 

    window.addeventlistener("domcontentloaded", filldatalist, false);

} ());

as you can see you have a predefined array with the same cities options from the previous code sample. the filldatalist function is called when the dom content loaded event is fired. in the filldatalist , all you have to do is to get the container element (that must be a phrasing element ) and append to it the created datalist . this example can be changed easily to use data retrieved from the server-side.

datalist browser support

as in all new html5 elements, currently the datalist isn’t supported in all of the browsers. the datalist is supported in chrome from version 20, in firefox from version 4, in internet explorer from version 10 and in opera from version 9. you can go to the following web page to see where the datalist is supported in more details.

summary

the datalist element can be useful when you have predefined options that you want to use as autocomplete in html controls. it’s lack of browsers support forces us to have fallbacks to the behavior using javascript control libraries such as the autocomplete control in jquery ui.

Element HTML

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Role of Data Governance in Data Strategy: Part II
  • Memory Debugging: A Deep Level of Insight
  • Why Does DevOps Recommend Shift-Left Testing Principles?
  • How to Develop a Portrait Retouching Function

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: