
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to refer to a
The HTML <datalist> tag is used to add autocomplete functionality to form elements. It offers customers a set of predefined options from which to choose data. A "list" attribute containing "input" element should be used with the "<datalist>" tag. The datalist id and the value of the "list" attribute are related.
Following are the examples…
Example
In the following example we are using datalist to allow pre defined option for the user.
If you press A, it will show a list of cricketers starting with A letter.
<!DOCTYPE html> <html> <body> <label> Enter your favorite cricket player:<br /> <input type="text" id="players" list="Players"> <datalist id="Players"> <option value="MSD"> <option value="Virat"> <option value="Sangakara"> <option value="Sehwag"> <option value="Watson"> <option value="Raina"> <option value="Hussey"> <option value="ABD"> </datalist> </label> </body> </html>
Output
On executing the above script, you will come up with a data list containing different players, and if you press any alphabet, it will list down the names that contain the alphabet that was pressed.
Example
Another example can be seen as −
<!DOCTYPE html> <html> <head> <title>HTML Datalist Tag</title> </head> <body> <input list = "tutorials" /> <datalist id = "tutorials"> <option value = "Java"> <option value = "ASP"> <option value = "PHP"> <option value = "Ruby"> <option value = "jQuery"> </datalist> <p>Try to add any of the tutorial name mentioned above.</p> </body> </html>
Output
The output obtained after executing the script above is as follows −
Example: Date and Time Types
Using the datalist tag, selecting the types like month, date, time, local time etc will be made easier to choose from the dropdown list.
<!DOCTYPE html> <html> <head> <body> <input type="time" list="Hours"> <datalist id="Hours"> <option value="10:00"> <option value="11:00"> <option value="12:00"> </datalist> </body> </head> </html>
Output
The output obtained after executing the above script is as follows −
Example: Range Types
Using the datalist tag, selecting the values in a particular range will be made possible using a slider represented using multiple hash marks at each recommended value.
<!DOCTYPE html> <html> <head> <body> <input type="range" list="tickmarks"> <datalist id="tickmarks"> <option value="0"> <option value="10"> <option value="20"> <option value="30"> </datalist> </body> </head> </html>
Output
The output obtained after executing the above script is as follows −
Example: Color Types
The datalist tag allows a user to pick a predefined color from a list of various colors.
<!DOCTYPE html> <html> <head> <body> <input type="color" list="Colors"> <datalist id="Colors"> <option value="#000000"> <option value="#FFFFFF"> <option value="#FFF000"> <option value="#A11112"> </datalist> </body> </head> </html>
Output
The output obtained after executing the above script is as follows −
Example: Password Types
The datalist tag allows a user to pick a randomly generated password from a list of various passwords. But no browser usually prefers it due to security reasons.
<!DOCTYPE html> <html> <head> <body> <input type="password" list="randomPassword"> <datalist id="randomPassword"> <option value="5Mg[_3DnkgSu@!q#"> </datalist> </body> </head> </html>
Output
The output obtained after executing the above script is as follows −
- Related Articles
- HTML DOM Datalist options Collection
- How to specify that an element should be pre-selected when the page loads in HTML?
- How to test if an element contains class in JavaScript in HTML?
- How to add a regular expression that an input element's value is checked against in HTML?
- How to add a unique id for an element in HTML?
- How to specify a unique id for an element in HTML?
- How to use inline CSS style for an element in HTML?
- How to check if a slice contains an element in Golang?
- How to delete a list element that only contains NA in R?
- How to specify that an element is not yet relevant in HTML?
- How to add an address element in HTML?
- How to add attributes to an HTML element in jQuery?
- How to specify that the styles only apply to this element's parent element and that element's child elements in HTML?
- How do I find an element that contains specific text in Selenium Webdriver?
- CSS to put icon inside an input element in a form
