126

I am trying to make a dropdown list that contains a placeholder. It doesn't seem to support placeholder="stuff" as other forms do. Is there a different way to obtain a placeholder in my dropdown?

20 Answers 20

273

Yes just "selected disabled" in the option.

<select>
    <option value="" selected disabled>Please select</option>
    <option value="">A</option>
    <option value="">B</option>
    <option value="">C</option>
</select>

Link to fiddle

You can also view the answer at

https://stackoverflow.com/a/5859221/1225125

2
  • 6
    This doesn't answer the quesition as he/she is asking for a bootstrap built-in solution
    – Mehdi
    Apr 1, 2016 at 2:08
  • 5
    Also You can use disabled selected hidden or only hidden all are correct. :)
    – MD Ashik
    Dec 1, 2016 at 6:04
75

Use hidden:

<select>
    <option hidden >Display but don't show in list</option>
    <option> text 1 </option>
    <option> text 2 </option>
    <option> text 3 </option>
</select>
1
  • continue with the last comment, when use v-model, needs to use this: <option :value="null" disabled hidden>Select Age</option>
    – Homer
    Jan 31, 2019 at 4:45
16

dropdown or select doesn't have a placeholder because HTML doesn't support it but it's possible to create same effect so it looks the same as other inputs placeholder

    $('select').change(function() {
     if ($(this).children('option:first-child').is(':selected')) {
       $(this).addClass('placeholder');
     } else {
      $(this).removeClass('placeholder');
     }
    });
.placeholder{color: grey;}
select option:first-child{color: grey; display: none;}
select option{color: #555;} // bootstrap default color
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control placeholder">
   <option value="">Your Placeholder Text</option>
   <option value="1">Text 1</option>
   <option value="2">Text 2</option>
   <option value="3">Text 3</option>
</select>

if you want to see first option in list remove display property from css

2
  • 8
    You wrote a ton of custom code when adding "selected disabled" class is the same thing.
    – Jordan.J.D
    Aug 29, 2014 at 14:21
  • 2
    To his credit, his is a little more visually complete (with more code), where the placeholder is not rendered within the list selection. Though I'm going for the minimal code way of things.
    – Roadkillnz
    Dec 24, 2014 at 1:44
12

Most of the options are problematic for multi-select. Place Title attribute, and make first option as data-hidden="true"

<select class="selectpicker" title="Some placeholder text...">
    <option data-hidden="true"></option>
    <option>First</option>
    <option>Second</option>
</select>
1
  • 2
    This is correct answer if you are using bootstrap-select JS plugin. By default in my dotnet core webapp it was not allowing me to select the first item. But after using this work around and for the select element instead of using 'placeholder' when I used 'title' as suggested, it worked perfect! Thanks.
    – sanpat
    Jan 25, 2021 at 18:00
11

If you are initializing the select field through javascript, the following can be added to replace the default placeholder text

noneSelectedText: 'Insert Placeholder text'

example: if you have:

<select class='picker'></select>

in your javascript, you initialize the selectpicker like this

$('.picker').selectpicker({noneSelectedText: 'Insert Placeholder text'});  
1
  • I would think this is the best answer YTD.
    – xtlc
    Apr 12, 2021 at 23:29
7

Add hidden attribute:

<select>
    <option value="" selected disabled hidden>Please select</option>
    <option value="">A</option>
    <option value="">B</option>
    <option value="">C</option>
</select>
2
  • How this is different from Jay Lin's answer and Brakke's answer ?
    – Athafoud
    Mar 15, 2016 at 10:43
  • 1
    This worked the best for me, because selected meaning it's selected by default, disabled so it can't be clicked, and hidden from the dropdown. I do suppose if hidden is used, disabled probably doesn't need to be in there. Oct 3, 2018 at 23:38
5

Try this:

<select class="form-control" required>
<option value="" selected hidden>Select...</option>

when using required + value="" then user can not select it using hidden will make it hidden from the options list, when the user open the options box

2
  • highly recommended!
    – Aqua 4
    Apr 16, 2020 at 13:57
  • Does not gray the text. Adding class="text-muted" to the option does not help. Adding disabled prevents it from being auto-selected.
    – user9645
    Jan 7, 2021 at 15:07
2

Try @title = "stuff". It worked for me.

1
  • 4
    An example would fit pefectly Jun 14, 2015 at 21:31
2

All this options are.... improvisations. Bootstrap already offers a solution for this. If you want to change the placeholder for all your dropdown elements you can change the value of

noneSelectedText in the bootstrap file.

To change individualy, you can use TITLE parameter.

example:

<div class="form-group">
          <label class="control-label col-xs-2" id="country">Continent:</label>
          <div class="col-xs-9">
            <select name="zones[]" class="selectpicker" title="All continents" id="zones" multiple >
              <option value="Africa">Africa</option>
              <option value="Asia">Asia</option>
              <option value="North America">America North</option>
              <option value="South America">America South</option>
              <option value="Antarctica">Antarctica</option>
              <option value="Australia">Australia</option>
              <option value="Europe">Europe</option>
            </select>
          </div>
        </div>
1
  • You sir just saved me. All the above options were not working for multiselect. As the select was not de-selecting previous values. Title worked for me
    – Faran Khan
    Aug 29, 2018 at 11:45
1

Using Bootstrap, This is what you can do. Using class "text-hide", the disabled option will be shown at first but not on the list.

<select class="form-control" >
  <option selected disabled class="text-hide">Title</option>
  <option>A</option>
  <option>B</option>
  <option>C</option>
</select>
1

Bootstrap select has an noneSelectedText option. You can set it via data-none-selected-text attribute. Documentation.

1
  1. in bootstrap-select.js Find title: null, and remove it.
  2. add title="YOUR TEXT" in <select> element.
  3. Fine :)

Example:

<select title="Please Choose one item">
    <option value="">A</option>
    <option value="">B</option>
    <option value="">C</option>
</select>
1

I think, the Dropdown box with a class and JQuery code to disable the first option for user to select, will work perfectly as Select Box placeholder.

<select class="selectboxclass">
   <option value="">- Please Select -</option>
   <option value="IN">India</option>
   <option value="US">America</option>
</select>

Make the first option disabled by JQuery.

<script>
$('select.selectboxclass option:first').attr('disabled', true);
</script>

This will make the first option of Dropdown as Placeholder and user will no longer able to select the first option.

Hope It helps!!

1

This is for Bootstrap 4.0, you only need to enter selected on the first line, it acts as a placeholder. The values are not necessary, but if you want to add value 0-... that is up to you. Much simpler than you may think:

<select class="custom-select">
   <option selected>Open This</option>
   <option value="">1st Choice</option>
   <option value="">2nd Choice</option>
</select>

This is link will guide you for further information.

1

The right way to achieve what you are looking for is to use the title attribute. The title global attribute contains text representing advisory information related to the element it belongs to.

title="Choose..."

for example:

<select class="form-control selectpicker" name="example" title="Choose...">
    <option value="all">ABCDEFG</option>
    <option value="all">EFG</option>
</select>

check the documentation for more info about bootstrap-select

0

Here's another way to do it

<select name="GROUPINGS[xxxxxx]" style="width: 60%;" required>
    <option value="">Choose Platform</option>
<option value="iOS">iOS</option>
    <option value="Android">Android</option>
    <option value="Windows">Windows</option>
</select>

"Choose Platform" becomes the placeholder and the 'required' property ensures that the user has to select one of the options.

Very useful, when you don't want to user field names or Labels.

0

For .html page

<select>
    <option value="" selected disabled>Please select</option>
    <option value="">A</option>
    <option value="">B</option>
    <option value="">C</option>
</select>

for .jsp or any other servlet page.

<select>
    <option value="" selected="true" disabled="true">Please select</option>
    <option value="">A</option>
    <option value="">B</option>
    <option value="">C</option>
</select>
0

Using bootstrap, if you need to also add some values to the option to use for filters or other stuff you can simply add the class "bs-title-option" to the option that you want as a placeholder:

<select class="form-group">
   <option class="bs-title-option" value="myVal">My PlaceHolder</option>
   <option>A</option>
   <option>B</option>
   <option>c</option>
</select>

Bootstrap adds this class to the title attribute.

0

Solution for Angular 2

Create a label on top of the select

<label class="hidden-label" for="IsActive"
    *ngIf="filterIsActive == undefined">Placeholder text</label>
<select class="form-control form-control-sm" type="text" name="filterIsActive"
    [(ngModel)]="filterIsActive" id="IsActive">
    <option value="true">true</option>
    <option value="false">false</option>
</select>

and apply CSS to place it on top

.hidden-label {
    position: absolute;
    margin-top: .34rem;
    margin-left: .56rem;
    font-style: italic;
    pointer-events: none;
}

pointer-events: none allows you to display the select when you click on the label, which is hidden when you select an option.

0
<option value="" defaultValue disabled> Something </option>

you can replace defaultValue with selected but that would give warning.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.