Learn HTML

HTML: datalist tag

 

Description of HTML datalist tag

The HTML datalist tag is used to create a list of predefined options for an input element. The datalist tag is typically used together with the input tag, which allows users to select one of the predefined options or enter their own value.

Here’s an example of how the datalist tag can be used with an input element:

<label for="fruits">Choose a fruit:</label>
<input list="fruits" name="fruit">
<datalist id="fruits">
<option value="Apple">
<option value="Banana">
<option value="Cherry">
<option value="Date">
</datalist>

In this example, the datalist tag is used to define a list of four fruit options: Apple, Banana, Cherry, and Date. The input tag has a list attribute that points to the id of the datalist tag, which allows the user to select one of the predefined options.

The datalist tag is a block-level element and can contain one or more option tags, each with a value attribute that specifies the text to be displayed as an option.

Some common attributes used with the datalist tag include id, class, lang, style, and title, among others. The input tag can also have additional attributes such as name, type, placeholder, required, and autocomplete, among others, depending on the specific use case.

Note that not all browsers support the datalist tag, so it’s important to provide a fallback option for users who are using older browsers.


 

 

Syntax of HTML datalist tag

The HTML datalist tag is used to provide a list of predefined options for an input field. The syntax of the datalist tag is as follows:

<input type="text" list="datalist-id">
<datalist id="datalist-id">
<option value="Option 1">
<option value="Option 2">
<option value="Option 3">
...
</datalist>

Here, the input tag creates a text input field with a list attribute that specifies the ID of the datalist element. The datalist element then provides a list of predefined options using the option tag. Each option tag should have a value attribute that specifies the option’s value.

The datalist tag can also have attributes that can be used to modify its behavior and appearance. For example, the class and id attributes can be used to apply CSS styles or to target the datalist element with JavaScript.

Example:

<label for="fruits">Choose a fruit:</label>
<input type="text" id="fruits" list="fruits-list">
<datalist id="fruits-list">
<option value="Apple">
<option value="Banana">
<option value="Cherry">
<option value="Grape">
</datalist>

This will create a text input field with a list of four fruits: “Apple”, “Banana”, “Cherry”, and “Grape”. When the user types into the input field, the browser will show a dropdown list of available options based on the values in the datalist element.


 

Global Attributes

The <datalist> tag is used to provide a predefined list of options for an <input> element. It can be used in conjunction with the <input> element’s list attribute, which references the ID of the <datalist> element.

There are a few global attributes that can be used with the <datalist> tag:

  • id: Specifies a unique ID for the <datalist> element. This ID is used to associate the <datalist> element with the <input> element’s list attribute.
  • class: Specifies one or more class names for the <datalist> element, which can be used to apply CSS styles to the element.
  • style: Specifies inline CSS styles for the <datalist> element.
  • title: Specifies a text description of the <datalist> element, which can be used by screen readers or other assistive technology to provide additional information to users.
  • lang: Specifies the language of the content within the <datalist> element.
  • dir: Specifies the directionality of the content within the <datalist> element (either “ltr” for left-to-right or “rtl” for right-to-left).
  • accesskey: Specifies a shortcut key that can be used to navigate to the <datalist> element.
  • tabindex: Specifies the tab order of the <datalist> element within the document.
  • hidden: Specifies that the <datalist> element should be hidden from view.

Event Attributes

The <datalist> tag does not support any specific event attributes. However, you can use event attributes on the <input> element that references the <datalist> using the list attribute.

Some common event attributes that can be used with the <input> element include:

  • oninput: This event fires when the user types into the input field associated with the datalist. You can use this event to trigger a JavaScript function that updates the list of options displayed in the datalist dynamically based on user input.
  • onblur: This event fires when the input field associated with the datalist loses focus. You can use this event to validate the user’s input or perform some other action when the user has finished typing.
  • onfocus: This event fires when the input field associated with the datalist receives focus. You can use this event to perform some action or display a message when the user starts typing in the input field.
  • onchange: This event fires when the user selects an option from the datalist. You can use this event to trigger a JavaScript function that performs some action based on the user’s selection.

It’s worth noting that some of these events may not be supported by all browsers, so it’s important to test your code in multiple browsers to ensure that it works as expected.

 

Other Attributes

The <datalist> tag does not have any attributes other than the global attributes that are available to all HTML elements. However, it does have two child elements that can be used to define the list of options:

  • <option>: This element is used to define an option in the datalist. The value attribute of the <option> element specifies the value that will be displayed in the input field when the user selects that option.
  • <label>: This element can be used to provide a label for an option in the datalist. The for attribute of the <label> element should match the id attribute of the corresponding <option> element.

Here’s an example of how you might use these child elements with the <datalist> tag:

<label for="fruit">Select a fruit:</label>
<input list="fruits" id="fruit" name="fruit" />

<datalist id="fruits">
<option value="Apple">
<label for="apple">Apple</label>
</option>
<option value="Banana">
<label for="banana">Banana</label>
</option>
<option value="Cherry">
<label for="cherry">Cherry</label>
</option>
</datalist>

In this example, we’ve defined a datalist with three options: Apple, Banana, and Cherry. We’ve also provided labels for each option using the <label> element, so that users can more easily understand the options that are available to them. Finally, we’ve associated the datalist with an input field using the list attribute on the input element.


 

 

Notes on HTML: datalist tag

Here are a few important notes on using the <datalist> tag in HTML:

  1. Browser support: The <datalist> tag is supported by most modern web browsers, including Chrome, Firefox, Safari, and Edge. However, some older browsers may not support this tag, so it’s important to test your code in multiple browsers to ensure that it works as expected.
  2. Accessibility: When using the <datalist> tag, it’s important to provide clear and descriptive labels for each option using the <label> element. This can help users who rely on assistive technology, such as screen readers, to understand the options that are available to them.
  3. Styling: The appearance of the datalist and its options can be styled using CSS. However, the appearance of the datalist may differ across different browsers and operating systems. To ensure a consistent appearance, you may need to use CSS to style the <input> element that references the datalist.
  4. Data validation: The <datalist> tag does not provide any built-in data validation, so it’s important to validate user input on the server-side using a server-side scripting language, such as PHP or Python.
  5. Security: The <datalist> tag can be vulnerable to cross-site scripting (XSS) attacks if user input is not properly sanitized. To prevent XSS attacks, you should always sanitize user input and validate it on the server-side before using it in your application.

 


 

Browser Compatibility

The HTML datalist tag has essential support with the following browsers:

  1. Chrome
  2. Internet Explorer (IE)
  3. Opera
  4. Safari (WebKit)
  5. Firefox (Gecko)
  6. Android
  7. Firefox Mobile (Gecko)
  8. Edge Mobile
  9. Opera Mobile
  10. Safari Mobile

 

Examples of HTML datalist tag

We will discuss the HTML datalist tag below, with some examples of how to use the datalist tag in HTML5, HTML 4.01 Transitional, XHTML 1.0 Strict, XHTML 1.0 Transitional, and XHTML 1.1.

 


 

HTML 4.01 Transitional Document

I’m sorry, but the <datalist> tag is not available in HTML 4.01, as it was introduced in HTML5. If you need to use a dropdown list in an HTML 4.01 document, you can use a combination of the <select> and <option> tags to create a similar effect. Here’s an example:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Datalist Example</title>
</head>
<body>
<h1>Choose Your Favorite Car:</h1>
<form>
<label for="car">Car:</label>
<select id="car" name="car">
<option value="" selected="selected">Choose a car...</option>
<option value="Toyota">Toyota</option>
<option value="BMW">BMW</option>
<option value="Mercedes">Mercedes</option>
<option value="Audi">Audi</option>
<option value="Honda">Honda</option>
</select>
</form>
</body>
</html>

In this example, we have used the <select> tag to create a dropdown list of car options. The <option> tags inside the <select> tag define the available options for the user to choose from. The selected="selected" attribute on the first <option> tag sets it as the default option that is displayed when the page is loaded.

To associate the <label> with the <select> tag, we have used the for attribute on the <label> tag with a value of “car”, which matches the id attribute of the <select> tag.

While this method may not have all the features of the <datalist> tag, it can still provide a basic dropdown list for users to select options from.

 

 

XHTML 1.0 Transitional Document

Here’s an example of how to use the <datalist> tag in an XHTML 1.0 Transitional document:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Datalist Example</title>
<style type="text/css">
input[type="text"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
width: 300px;
}
</style>
</head>
<body>
<h1>Choose Your Favorite Car:</h1>
<label for="car">Car:</label>
<input type="text" id="car" name="car" list="carList" placeholder="Type to select" />
<datalist id="carList">
<option value="Toyota">
<label for="Toyota">Toyota</label>
</option>
<option value="BMW">
<label for="BMW">BMW</label>
</option>
<option value="Mercedes">
<label for="Mercedes">Mercedes</label>
</option>
<option value="Audi">
<label for="Audi">Audi</label>
</option>
<option value="Honda">
<label for="Honda">Honda</label>
</option>
</datalist>
</body>
</html>

his example is very similar to the HTML5 example I provided earlier, with a few minor differences to make it valid XHTML 1.0 Transitional.

The <?xml version="1.0" encoding="UTF-8"?> declaration at the top of the document specifies that the document is encoded in UTF-8, which is a common character encoding for XHTML documents.

The <!DOCTYPE> declaration specifies that the document is using the XHTML 1.0 Transitional DTD. The xmlns attribute on the <html> tag specifies the default namespace for the document.

The <input> and <datalist> tags are self-closing to conform with the stricter rules of XML syntax. The <br> tag has also been replaced with a self-closing tag.

Aside from these minor differences, the code for using the <datalist> tag in an XHTML document is virtually identical to using it in an HTML5 document.

 

 

XHTML 1.0 Strict Document

Here’s an example of how to use the <datalist> tag in an XHTML 1.0 Strict document:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Datalist Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
input[type="text"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
width: 300px;
}
</style>
</head>
<body>
<h1>Choose Your Favorite Car:</h1>
<label for="car">Car:</label>
<input type="text" id="car" name="car" list="carList" placeholder="Type to select" />
<datalist id="carList">
<option value="Toyota">
<label for="Toyota">Toyota</label>
</option>
<option value="BMW">
<label for="BMW">BMW</label>
</option>
<option value="Mercedes">
<label for="Mercedes">Mercedes</label>
</option>
<option value="Audi">
<label for="Audi">Audi</label>
</option>
<option value="Honda">
<label for="Honda">Honda</label>
</option>
</datalist>
</body>
</html>

The changes from the XHTML 1.0 Transitional example are minimal, but important for strict XHTML compliance. Here are the differences:

  • The <!DOCTYPE> declaration has been changed to specify the XHTML 1.0 Strict DTD.
  • The meta tag for specifying the character encoding has been changed to use the http-equiv attribute instead of the charset attribute.
  • The <br> tag has been replaced with a self-closing tag.
  • The input and datalist tags have been made self-closing.

Other than these changes, the code for using the <datalist> tag in an XHTML 1.0 Strict document is identical to the code for using it in an XHTML 1.0 Transitional or HTML5 document.

 

XHTML 1.1 Document

Here’s an example of how to use the <datalist> tag in an XHTML 1.1 document:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Datalist Example</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<style type="text/css">
input[type="text"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
width: 300px;
}
</style>
</head>
<body>
<h1>Choose Your Favorite Car:</h1>
<label for="car">Car:</label>
<input type="text" id="car" name="car" list="carList" placeholder="Type to select" />
<datalist id="carList">
<option value="Toyota">
<label for="Toyota">Toyota</label>
</option>
<option value="BMW">
<label for="BMW">BMW</label>
</option>
<option value="Mercedes">
<label for="Mercedes">Mercedes</label>
</option>
<option value="Audi">
<label for="Audi">Audi</label>
</option>
<option value="Honda">
<label for="Honda">Honda</label>
</option>
</datalist>
</body>
</html>

 

There are a few key differences between this example and the previous examples:

  • The <!DOCTYPE> declaration has been changed to specify the XHTML 1.1 DTD.
  • The meta tag for specifying the character encoding has been changed to specify that the content is in application/xhtml+xml format.
  • The input and datalist tags have been made self-closing.

Note that in an XHTML 1.1 document, all elements must be properly nested and closed, and all attributes must be quoted. In addition, XHTML 1.1 documents require that the document be well-formed XML, which means that all tags must be lowercase and all attributes must be in quotes.

 

About the author

Home4Cloud