HTML Basics

HTML Introduction HTML Basic HTML Comments HTML Tags/Elements HTML Attributes HTML Formatting HTML Block and Inline HTML Charsets HTML Classes HTML Colors HTML Div HTML Headings HTML Id HTML Iframes HTML Images HTML File Paths HTML Tables HTML Layout HTML Lists HTML Links <a> HTML Paragraphs HTML Quotations HTML JavaScript HTML Emojis HTML URL Encode HTML Entities HTML Computercode HTML Symbols HTML Styles

HTML Forms

HTML Forms HTML Form Elements HTML Form Attributes HTML Input Attributes HTML Input Regex HTML Input Form Attributes HTML Input Types

HTML SEO

HTML Head HTML Page Title HTML Responsive HTML Semantics HTML Favicon

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Audio HTML Video

HTML Reference

a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote body br button canvas caption center cite code col colgroup data datalist dd del details dfn dialog dir div dl DOCTYPE dt em embed fieldset figcaption figure font footer form frame frameset h1_-_h6 head header hgroup hr html i iframe img input ins kbd label legend li link main map mark menu meta meter nav noframes noscript object ol optgroup option output p param picture pre progress q rp rt ruby s samp script search section select small source span strike strong style sub summary sup svg table tbody td template textarea tfoot th thead time title tr track tt u ul var video wbr

HTML <datalist> Tag

The <datalist> tag in HTML is used to provide a pre-defined list of options for <input> elements. This allows users to input data by choosing from a list of options, rather than typing it out manually. The options in the datalist can be suggested as the user types in the input field, making it a useful feature for forms and search functions.

How to Use the <datalist> Tag

To use the <datalist> tag, you need to follow these steps:

  1. Create an input field with the <input> tag.
  2. Add a list attribute to the <input> tag and set it to the ID of the <datalist> element.
  3. Create a <datalist> element with an ID that matches the list attribute in the <input> element.
  4. Add <option> elements inside the <datalist> element to provide the list of options.

Main Attributes of the <datalist> Tag

Attribute Description
id Sets the ID of the <datalist> element, which is used to link it to an <input> element.

Example: Using the <datalist> Tag

Let's create an example where we have an input field for selecting a country from a list of options:


<input type="text" list="countries">
<datalist id="countries">
  <option value="USA">
  <option value="Canada">
  <option value="UK">
  <option value="Australia">
</datalist>

In this example, as the user types in the input field, a list of country options will be suggested based on the <datalist> element.