HTML5 DataList Example

HTML5 Datalist is a new html tag shipped with HTML5 specification. HTML5 Datalist can be used to create a simple poor man’s Autocomplete feature for a webpage.


In this tutorial we will go through the basics of HTML5 Datalist tag and check some quick examples of autocomplete.


Introduction to HTML5 Datalist tag


As part of HTML5 specification a new tag <datalist> has been introduced. Using this tag, we can define a list of data which then can be used as list for an input box. We can create a simple Autocomplete feature using this tag.


html5-datalist-example


Consider a textbox to take country name input from user.


Syntax:



<input type="text" list="countries" name="mycountry" />

See how we used attribute list and pass a list of countries called ‘countries’. We can define this list of countries using datalist tag.



<datalist id="countries">
<option value="India">India</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Germany">Germany</option>
<option value="France">France</option>
</datalist>

So we created a list of countries using <datalist> tag. The id of datalist tag must match with the list attribute of the input box.


Demo


Below is the demo. Enter some text in the Country textbox and see how an autocomplete list appears below it.



Browser Compatibility


Not every browser out there supports html5 datalist for now. So far IE 10 and above, Firefox, Chrome and Opera 19 above supports html5 datalist tag. Safari doesn’t support datalist yet :(



















ChromeFirefox (Gecko)Internet ExplorerOperaSafari
3126.010.019.0

This might change in future. Check the latest compatibility matrix for HTML5 Datalist here http://ift.tt/V2Oe3G.


Backward Compatibility for HTML5 Datalist tag


As seen in above compatibility table, not all browser supports html5 datalist tag. If you are using datalist, the older browser will simply ignores the datalist tag and renders rest of the webpage. The textbox which is linked to datalist will remain just a textbox. No autocomplete will be added. Internet explorer 9 and below has unique way of parsing datalist. If you try to check above example in IE 9 or below you will see all the option values are simply printed on the page. To avoid this we can wrap the options in datalist tag with a select tag. For example:



<datalist id="countries">
<select>
<option value="India">India</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Germany">Germany</option>
<option value="France">France</option>
</select>
</datalist>

This can give user option to select from the list if autocomplete is not available. Check below screenshot.


html5-datalist-backward-compatibility


Here we simple let user enter the country name or they can select from the country list box. This way users have an option to select from the list if they want to.


This can be simply achieve by adding a select tag inside datalist and make it as if it’s a select box.


Demo:



Here we simply wrapped all options within select tag. The name of select tag must be same as that of textbox. So when the form is submitted the appropriate value is passed to server-side code.


This is crud way of doing backward compatibility. If you don’t want to add more javascripts to your page, you can use this simple technique.


There is a better way of adding datalist compatibility to browsers that aren’t yet supporting this feature. We call it Polyfills. It is a way to fallback functionality to older browsers.


Chris Coyier has written a nice library that polyfills the datalist capabilities into older/non-supportive browsers.


All we need to do is to check if our browser supports datalist of not. If not then simply includes the polyfill js library. Check out below example.


We use Modernizr to identify if browser supports html5 datalist, if not then we simply includes the polyfill javascript for datalist by Chris Coyier.



<!DOCTYPE html>
<html>
<head>
<script src="http://ift.tt/1bLNnMy;

<script>

// Safari reports success of list attribute, so doing ghetto detection instead
yepnope({
test : (!Modernizr.input.list || (parseInt($.browser.version) > 400)),
yep : [
'http://ift.tt/1kB95v4',
'http://ift.tt/1bLNpnP'
]
});
</script>
</head>
<body>
<input type="text" list="countries" name="mycountry" />

<datalist id="countries">
<option value="India">India</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Germany">Germany</option>
<option value="France">France</option>
</datalist>
</body>
</html>

Online Demo


View Demo


Download Source Code


html5-datalist.zip (5.4KB)


The post HTML5 DataList Example appeared first on ViralPatel.net.








via ViralPatel.net http://ift.tt/1kB961U

FluffyApp 3.0.2

FluffyApp is a simple and lightweight utility that was designed to help you instantly upload your pictures or other files to CloudApp. You only have to sign into your account and drag the files onto the application’s icon. CloudApp is a simple app which sits on the menu bar and allow drag-and-drop file uploading complete with an auto-shortened URL ...





via FileHippo.com http://ift.tt/1cBbkcx [[ We are also giving web service. Email:wasim.akh2@gmail.com]]

Mobile Atlas Creator 1.9.16

Bring your online maps to the offline world: Mobile Atlas Creator (MOBAC) creates offline atlases of online maps for your favorite mobile navigation program.





via FileHippo.com http://ift.tt/1cdfV5K [[ We are also giving web service. Email:wasim.akh2@gmail.com]]

HP says security updates not included under new firmware policy

Bitcoin exchanges hit by DDoS attacks

Marissa Mayer 'very excited' about contextual search

Wireless charging alliances teaming up to work toward a cable-free world

[Changelog] What’s New in Google Chrome 130 and Later Versions

UPDATE: Addition of Google Chrome 133.0 version. In this exclusive changelog article, we are providing information about all versions of Goo...