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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Example of a Dynamic HTML5 Datalist Control

Example of a Dynamic HTML5 Datalist Control

Raymond Camden user avatar by
Raymond Camden
·
Jul. 03, 12 · Interview
Like (0)
Save
Tweet
Share
38.56K Views

Join the DZone community and get the full member experience.

Join For Free

i've made no secret of being a huge fan of the updates to forms within html5. one of the more interesting updates is the datalist control. this control gives you basic autocomplete support for an input field. at its simplest, you create the datalist control options, tie it to a control, and when the user types, they see items that match your initial list. consider this example.

<!doctype html>
<html>
<head>
<title>example 0</title>
</head>

<body>

<p>
<input type="text" name="search" id="search" placeholder="type something" list="searchresults" autocomplete="off">
<datalist id="searchresults">
<option>ray</option>
<option>scott</option>
<option>todd</option>
<option>dave</option>
<option>jeanne</option>
<option>jacob</option>
</datalist>
</p>

</body>
</html>

note that the input field, search, has a list attribute that points to the datalist control below it. the datalist control is simply a list of options. if you run this code in a supported browser , you will see the options show up as autocomplete values. as you type, they filter out based on your input. if you run this in an unsupported browser, nothing bad happens. (you can demo this script yourself here .)

so - that's cool i think - but most folks are not going to have a static list of options. instead, they will want to base it on some dynamic data, possibly loaded in via ajax. i built a simple demo that talks to some server-side code and returns a list of options based on input. let's look at the code.

<!doctype html>
<html>
<head>
<title>example 1</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {

$("#search").on("input", function(e) {
var val = $(this).val();
if(val === "") return;
//you could use this to limit results
//if(val.length < 3) return;
console.log(val);
$.get("artservice.cfc?method=getart&returnformat=json", {term:val}, function(res) {
var datalist = $("#searchresults");
datalist.empty();
if(res.data.length) {
for(var i=0, len=res.data.length; i<len; i++) {
var opt = $("<option></option>").attr("value", res.data[i][0]);
datalist.append(opt);
}

}
},"json");
});

})
</script>	
</head>

<body>

<p>
<input type="text" name="search" id="search" placeholder="type something" list="searchresults" autocomplete="off">
<datalist id="searchresults"></datalist>
</p>

</body>
</html>

for my example, i make use of jquery, although that is certainly not required. i've bound to the input event for my search field and removed any items from my datalist control. when the event is fired, i grab the field value and simply pass it to a server-side script that does a database lookup. none of this is particular new or unique, but note how i handle the result. i take each result sent to me and create new option items for my datalist. (note too that i make sure to clear out any previous ones.) this then gives me a simple database-bound datalist control. you can try this yourself here . i recommend using "mo" for input.

one interesting tidbit. i noticed in chrome that if i didn't disable autocomplete, the browser would use both the explicit list i specified in javascript and my personal autocomplete history. it even used a little separator to divide the options:

i'm not sure if i like that so for my examples i've simply disabled it. firefox only shows the explicit list, which feels right to me.

as i mentioned above, if you run this in a browser that doesn't support datalist, it fails nicely. but my demo was still firing off a bunch of ajax requests and that seemed a bit silly. in my final version, i modified the code to first see if the browser supported datalist. this way it won't waste time hitting the server when it doesn't need to.

<!doctype html>
<html>
<head>
<title>example 2</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {

if(document.createelement("datalist").options) {

$("#search").on("input", function(e) {
var val = $(this).val();
if(val === "") return;
//you could use this to limit results
//if(val.length < 3) return;
console.log(val);
$.get("artservice.cfc?method=getart&returnformat=json", {term:val}, function(res) {
var datalist = $("#searchresults");
datalist.empty();
if(res.data.length) {
for(var i=0, len=res.data.length; i<len; i++) {
var opt = $("<option></option>").attr("value", res.data[i][0]);
datalist.append(opt);
}

}
},"json");
});

}

})
</script>	
</head>

<body>

<p>
<input type="text" name="search" id="search" placeholder="type something" list="searchresults" autocomplete="off">
<datalist id="searchresults"></datalist>
</p>

</body>
</html>

you can try this version by hitting the giant demo button below.

HTML

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Are the Benefits of Java Module With Example
  • Tracking Software Architecture Decisions
  • Introduction to Spring Cloud Kubernetes
  • The Power of Docker Images: A Comprehensive Guide to Building From Scratch

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: