In this tutorial, we’ll take a deep-dive into the little-used HTML5
Key Takeaways
- The HTML5 `
- Unlike `
- Browser support for `
- The `
- Enhancements such as AJAX can be integrated with `
What’s Wrong with
HTML5
- there are lots of options, such as countries or job titles
- the user wants to enter their own option which is not on the list
The obvious solution is an autocomplete control. This allows the user to enter a few characters, which limits the options available for quicker selection.
Developers often turn to one of the many JavaScript-powered solutions, but a custom autocomplete control is not always necessary. The HTML5
Picking your country from a list containing more than 200 options is an ideal candidate for an autocomplete control. Define a
<span><span><span><datalist</span> id<span>="countrydata"</span>></span> </span> <span><span><span><option</span>></span>Afghanistan<span><span></option</span>></span> </span> <span><span><span><option</span>></span>?land Islands<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Albania<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Algeria<span><span></option</span>></span> </span> <span><span><span><option</span>></span>American Samoa<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Andorra<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Angola<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Anguilla<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Antarctica<span><span></option</span>></span> </span> ...etc... <span><span><span></datalist</span>></span> </span>
The datalist’s id can then be referenced by a list attribute in any field:
<span><span><span><label</span> for<span>="country"</span>></span>country<span><span></label</span>></span> </span> <span><span><span><input</span> type<span>="text"</span> </span></span><span> <span>list<span>="countrydata"</span> </span></span><span> <span>id<span>="country"</span> name<span>="country"</span> </span></span><span> <span>size<span>="50"</span> </span></span><span> <span>autocomplete<span>="off"</span> /></span> </span>
Confusingly, it’s best to set autocomplete="off". This ensures the user is shown values in the
The result:
This is the default rendering in Microsoft Edge. Other applications implement similar functionality, but the look differs across platforms and browsers.
Using the label as a text child of an
<span><span><span><datalist</span> id<span>="countrydata"</span>></span> </span> <span><span><span><option</span>></span>Afghanistan<span><span></option</span>></span> </span> <span><span><span><option</span>></span>?land Islands<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Albania<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Algeria<span><span></option</span>></span> </span> <span><span><span><option</span>></span>American Samoa<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Andorra<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Angola<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Anguilla<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Antarctica<span><span></option</span>></span> </span> ...etc... <span><span><span></datalist</span>></span> </span>
Using a value attribute produces identical results:
<span><span><span><label</span> for<span>="country"</span>></span>country<span><span></label</span>></span> </span> <span><span><span><input</span> type<span>="text"</span> </span></span><span> <span>list<span>="countrydata"</span> </span></span><span> <span>id<span>="country"</span> name<span>="country"</span> </span></span><span> <span>size<span>="50"</span> </span></span><span> <span>autocomplete<span>="off"</span> /></span> </span>
Note: the closing /> slash is optional in HTML5, although it could help prevent coding errors.
You can also set a value according to a chosen label using either of the following formats.
Option 1:
<span><span><span><datalist</span> id<span>="mylist"</span>></span> </span> <span><span><span><option</span>></span>label one<span><span></option</span>></span> </span> <span><span><span><option</span>></span>label two<span><span></option</span>></span> </span> <span><span><span><option</span>></span>label three<span><span></option</span>></span> </span><span><span><span></datalist</span>></span> </span>
Option 2:
<span><span><span><datalist</span> id<span>="mylist"</span>></span> </span> <span><span><span><option</span> value<span>="label one"</span> /></span> </span> <span><span><span><option</span> value<span>="label two"</span> /></span> </span> <span><span><span><option</span> value<span>="label three"</span> /></span> </span><span><span><span></datalist</span>></span> </span>
In both cases, the input field is set to 1, 2 or 3 when a valid option is chosen, but the UI varies across browsers:
- Chrome shows a list with both the value and the label. Only the value remains once an option is chosen.
- Firefox shows a list with the label only. It switches to the value once an option is chosen.
- Edge shows the value only.
The following CodePen example shows all variations:
See the Pen
HTML5
Implementations will evolve but, for now, I’d advise you do not use a value and label since it’s likely to confuse users. (A workaround is discussed below.)
The
There are several implementation notes, but they won’t affect most usage. The worst that could happen is a field reverts to a standard text input.
If you absolutely must support IE9 and below, there’s a fallback pattern which uses a standard
<span><span><span><datalist</span> id<span>="countrydata"</span>></span> </span> <span><span><span><option</span>></span>Afghanistan<span><span></option</span>></span> </span> <span><span><span><option</span>></span>?land Islands<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Albania<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Algeria<span><span></option</span>></span> </span> <span><span><span><option</span>></span>American Samoa<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Andorra<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Angola<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Anguilla<span><span></option</span>></span> </span> <span><span><span><option</span>></span>Antarctica<span><span></option</span>></span> </span> ...etc... <span><span><span></datalist</span>></span> </span>
See the Pen
HTML5
In modern browsers, the
In IE9 and below, both the (very long)
Both values could be entered in old IEs. Your application must either:
- decide which is most valid, or
- use a small JavaScript function to reset one when the other is changed
Using
Chrome-based browsers can also apply
-
An input with the type of "date". The user can choose from a range of options defined as YYYY-MM-DD values but presented in their locale format.
-
An input with the type of "color". The user can choose from a selection of color options defined as six-digit hex values (three-digit values don’t work).
-
An input with a type of "range". The slider shows tick marks, although this doesn’t limit which value can be entered.
See the Pen
HTML5
If you ever struggled styling a
An can be styled as normal, but a linked
I hope this situation improves, but for now, a solution is proposed at MDN which:
- overrides the default browser behavior
- effectively treats the
The above is the detailed content of Lightweight Autocomplete Controls with the HTML5 Datalist. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

TheCSSPaintingAPIenablesdynamicimagegenerationinCSSusingJavaScript.1.DeveloperscreateaPaintWorkletclasswithapaint()method.2.TheyregisteritviaregisterPaint().3.ThecustompaintfunctionisthenusedinCSSpropertieslikebackground-image.Thisallowsfordynamicvis

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.
