Today I Learned - Rocky Kev

TIL using datalist for a pure HTML input suggestion

POSTED ON:

TAGS:

Web developers do not use the power of datalists enough.

It combines the power of a selector, with the ability to type out your choice using the keyboard.

<label for="ice-cream-choice">Choose a flavor:</label>
<input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" />

<datalist id="ice-cream-flavors">
<option value="Chocolate">
<option value="Coconut">
<option value="Mint">
<option value="Strawberry">
<option value="Vanilla">
</datalist>

Best of all, this is native! No JS to auto-complete!

How-To:
You bind the input and the datalist together.

Via the MDN

You can even bind colors!

<input type="color" list="redColors">
<datalist id="redColors">
<option value="#800000">
<option value="#8B0000">
<option value="#A52A2A">
<option value="#DC143C">
</datalist>

via 5 HTML Tricks Nobody is Talking About


Related TILs

Tagged:

TIL Gmail has a 102KB size-limit for HTML

PLACEHOLDER

TIL how Error correction works in HTML

You never get an 'Invalid Syntax' error on an HTML page. Browsers fix any invalid content and go on.

TIL what DOCTYPE means

tl;dr: DOCTYPE declaration in the first line of the HTML file, to instruct the browser to run the code in Standard mode.