ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

HTML5 Tutorial: Using a Datalist to Suggest Values for an Input Control

Updated on July 6, 2011

Improve your website forms with HTML5

Many new features in HTML5 make it a lot easier to create user-friendly forms in HTML5. The new datalist element in HTML5 allows you to suggest input values to the user. For example, if the user has to type the name of a country then the input control can suggest some country names as soon as the user has typed in some characters.

The suggested values are shown in a drop-down box below the input control. This used to take many lines of code in earlier versions of HTML because you had to create that drop-down box yourself entirely. It is much easier in HTML5 because you only have to tell the browser the values it can suggest. The browser will then take care of the rest.

In this tutorial I'll show you how to use a datalist to suggest input values to the user and I'll give several practical examples.

Example 1: Using a datalist with options

<label>First name: <input list="names"/></label>
<datalist id="names">
<option value="Barbara">
<option value="Deborah">
<option value="Karen">
<option value="Linda">
<option value="Lisa">
<option value="Mary">
<option value="Susan">
</datalist>

We have an input control where the user can enter a first name. We don't need to specify that the input control will contain text with type="text" because text is the default value for type in HTML5.

The list attribute is new in HTML5 and it allows you to link an input control to a datalist. The list attribute contains the id of a datalist element in that document.

The datalist element is also new in HTML5 and it can contain option elements. Each option element has an attribute value. In our example we have an option for each first name.

When the user types a character the browser will show the relevant names. For example, if you type in B or b then it will show a drop-down box below the input control with Barbara. The user can then select Babara which will be placed in the input control as if the user had typed it in himself. This saves the user some time and it prevents typing errors.

The suggested values depend on the web browser

Please note that the suggested values depend on the web browser.

For example, Firefox shows all values that contain what the user typed in (so 's' shows Lisa and Susan) but Opera shows all values that start with what the user typed in (so 's' shows only Susan).

Keep in mind that this is a basic way of giving suggestions to visitors of your website but don't rely on it that certain values will be shown when you type something in.

Example 2: Options with a label and a value

<label>Email: <input type="email" list="emails"/></label>
<datalist id="emails">
<option label="Barbara Johnson" value="bjohnson@example.com">
<option label="Lisa Johnson" value="ljohnson@example.com">
</datalist>

We can take this idea one step further by adding labels to the options.

In the previous example the suggested value would be placed in the input control but you can also enter a different value in the control. You can do this by having both a label and a value.

The label is shown as a suggestion to the user but the value is actually put in the input control when the user selects that suggestion. In some browsers both the label and the value are shown as suggestion.

In this second example we have an input control where you can enter an email address (note that type="email", also a new addition in HTML5). The user can type in the name of a person and, when selected, the email address of that user will be put in the input control.

Example 3: Using JavaScript to enable / disable suggestions

<label>Fruit or vegetable: <input type="text" list="fruit_vegetables" onkeypress="onUserInput(this);"/></label>
<datalist id="fruit_vegetables">
<option id="option_apple" value="Apple">
<option id="option_lemon" value="Lemon">
<option id="option_lime" value="Lime">
<option id="option_peach" value="Peach">
<option id="option_pear" value="Pear">
<option id="option_waterlemon" value="Watermelon">
</datalist>
<script type="text/javascript">
var apple = document.getElementById("option_apple");
apple.setAttribute('disabled', 'disabled');
</script>

In this example we use JavaScript to hide some suggestions so that they are not shown to the user. The value Apple is disabled by JavaScript and it is not suggested when the user types something in.

We have given each option a unique id so that we can refer to it in JavaScript. We need to set the attribute disabled for an option to make sure it is ignored when looking for suggestions. In this case we set the attribute to the value disabled with setAttribute (which expects the name of an attribute and the new value). You can use this approach to programmatically show or hide suggestions to the user.

You can show an option again by removing the attribute disabled:

apple.removeAttribute('disabled');

Join HubPages today to earn money online!

This article was written by Simeon Visser. I am earning money online by writing here at HubPages.com. Would you like to earn money online as well? Read the success stories and sign up today to get started!

Did you like this tutorial?

Use the Share button below to share it with your fellow web developers!

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)