Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set selected by default with HTML5 datalist

Tags:

html

With HTML one can set the default selected item like this:

<option value="blaah" selected>

But how to do it with HTML5 and DataList select?

like image 427
Kaspar L. Palgi Avatar asked Oct 18 '25 10:10

Kaspar L. Palgi


1 Answers

Not per se. Just set the value as normal.

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

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

Of course, clicks in the input will be filtered by the text already there, so users will have to delete it to get the list of suggestions back.

like image 134
Quentin Avatar answered Oct 19 '25 23:10

Quentin