I would like to have an input field that users can enter custom text value or choose from drop down. A regular <select>
only offers drop down options.
How can I make a <select>
accept custom value? For instance: Ford?
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
HTML5 has a built-in combo box. You create a text input
and a datalist
. Then you add a list
attribute to the input
, with a value of the id
of the datalist
.
Update: As of March 2019 all major browsers (now including Safari 12.1 and iOS Safari 12.3) support datalist
to the level needed for this functionality. See caniuse for detailed browser support.
It looks like this:
<input type="text" list="cars" />
<datalist id="cars">
<option>Volvo</option>
<option>Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</datalist>