20

Is it possible to set a default prompt when nothing is selected for select box using css? or is there an elegant way to deal with this other than

 <option>Select Here</option>?
Norks
  • 390
  • 1
  • 5
  • 18
  • Does this answer your question? [How do I make a placeholder for a 'select' box?](https://stackoverflow.com/questions/5805059/how-do-i-make-a-placeholder-for-a-select-box) – JohnK Jun 23 '20 at 18:58

5 Answers5

24

Try this:

<select>
    <option value="" disabled>Select one--</option>
    <option value="someVal1">Option 1</option>
    <option value="someVal2">Option 2</option>
    <option value="someVal3">Option 3</option>
    <option value="someVal4">Option 4</option>
</select>
user1429980
  • 6,872
  • 2
  • 43
  • 53
Zachary Kniebel
  • 4,686
  • 3
  • 29
  • 53
  • Note: I deleted my previous answer as @Quentin pointed out that I was mistaken about `` and I found other sources to refute my statement that it is not an element. However, I did find out that the inconsistent support across browsers (as alluded to by @Quentin) and the fact that they can only exist within ` – Zachary Kniebel Apr 13 '13 at 05:29
  • not bad at all... although i was hoping some kind of placeholder similar to input text.. but still helpful. thanks – Norks Apr 13 '13 at 05:32
  • No worries - sorry about the confusion :) – Zachary Kniebel Apr 13 '13 at 05:34
  • 1
    "the fact that they can only exist within ` – Paul D. Waite Jul 08 '13 at 12:09
  • 5
    also, you can add `selected="selected"` to have it be the default value as well. If the value gets changed, it won't be able to change back – cadlac Mar 30 '14 at 21:51
  • 1
    ``disabled`` [does not allow for validation to occur](http://stackoverflow.com/questions/30532482/html5-form-validation-with-required-and-disabled-element), and keeps the ``select`` *valid*. This answer is partially incorrect. Edited answer. – user1429980 Oct 02 '15 at 22:11
  • 2
    this is useless, the initial prompt is not selected on load – SuperUberDuper Jul 08 '16 at 10:49
  • 1
    @SuperUberDuper the OP specifically asked for how to set a default prompt when nothing is selected on load. This solution leverages default browser functionality that displays the text of the first option when none have been selected. Since it is a default value, I inferred that the value should not be selected by default. If you want it selected then combine it with the comment by cadlac and enjoy. To simply call the solution "useless" is incorrect, crass and frankly a bit rude. – Zachary Kniebel Jul 09 '16 at 17:18
  • Ah yes, sorry I didn't see cadlacs answer. +1 – SuperUberDuper Jul 10 '16 at 12:53
8

The accepted answer did not work for me as expected.

If you need the "Select one option" to be the selected one by default , this worked for me

<select id="selected_option" name="selected_option">
     <option value="default" selected="selected">Select one option </option>
     <option value="someVal1">Option 1</option>
     <option value="someVal2">Option 2</option>
     <option value="someVal3">Option 3</option>
     <option value="someVal4">Option 4</option>
</select>

Is also important that the select has id and name so when you submit the form the value will be part of the POST or GET

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
4

                        <select>
                            <option style="display:none">Empty</option>
                            <option>button1</option>
                            <option>button2</option>
                        </select>
S1lllver
  • 191
  • 7
3

Try using option with unset value

<select>    
    <option value>Select something</option>
    <option value="1">Foo</option>
    <option value="2">Bar</option>
</select>
SpadgeAki
  • 77
  • 5
3

This is how I use for setup the default prompt but it will not set a default value. It use a HTML5 validation required. It may be a little bit shift from the question but I hope it could help as alternative.

<select id="car" name="car[brand]" required>
  <option disabled selected>Select Car Brand</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
worrawut
  • 958
  • 10
  • 17