Alamin Alamin - 5 months ago 14
HTML Question

How to add a title to a html select tag

How would I go about setting a title in select tag? Here is my select box:

<select>
<option value="sydney">Sydney</option>
<option value="melbourne">Melbourne</option>
<option value="cromwell">Cromwell</option>
<option value="queenstown">Queenstown</option>
</select>


When I visit the site, by default it shows "Sydney". But I want to display a title, such as, "What is the name of your city?"

kba kba
Answer
<select>
    <option selected disabled>Choose one</option>
    <option value="sydney">Sydney</option>
    <option value="melbourne">Melbourne</option>
    <option value="cromwell">Cromwell</option>
    <option value="queenstown">Queenstown</option>
</select>

Using selected and disabled will make "Choose one" be the default selected value, but also make it impossible for the user to actually select the item, like so:

Dropdown menu