132

I have a select which initially shows Select language until the user selects a language. When the user opens the select, I don't want it to show a Select language option, because it's not an actual option.

How can I achieve this?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Vitalii Ponomar
  • 10,686
  • 20
  • 60
  • 88

9 Answers9

258

Kyle's solution worked perfectly fine for me so I made my research in order to avoid any Js and CSS, but just sticking with HTML. Adding a value of selected to the item we want to appear as a header forces it to show in the first place as a placeholder. Something like:

<option selected disabled>Choose here</option>

The complete markup should be along these lines:

<select>
    <option selected disabled>Choose here</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
    <option value="5">Five</option>
</select>

You can take a look at this fiddle, and here's the result:

enter image description here

If you do not want the sort of placeholder text to appear listed in the options once a user clicks on the select box just add the hidden attribute like so:

<select>
    <option selected disabled hidden>Choose here</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
    <option value="5">Five</option>
</select>

Check the fiddle here and the screenshot below.

enter image description here

Community
  • 1
  • 1
Aurelio
  • 24,702
  • 9
  • 60
  • 63
  • This is a great answer but when using Angular JS, then it doesn't work since Angular automatically adds an empty option and sets it as the default – Dennis Wanyonyi Sep 14 '16 at 10:10
  • Anyway, just for reference, here's how you would do in Angular, just combine the above approach with the Angular specific explained here http://www.metaltoad.com/blog/angularjs-adding-user-friendly-ng-options-default – Aurelio Sep 22 '16 at 16:17
  • Note that [the `hidden` attribute](https://html.spec.whatwg.org/multipage/interaction.html#the-hidden-attribute), used in the answer above, is an attribute that can be set on any HTML element, and is usually (but not necessarily) implemented to be equivalent to setting `display: none` with CSS. – Mark Amery Jan 02 '17 at 12:47
  • 3
    Note also that setting `hidden` on an `option` in a `select` doesn't work in Safari, on either iOS or Mac. This answer thus offers only partial browser support. – Mark Amery Jan 03 '17 at 13:16
  • Still, this is an attempt at solving the problem at hand using best practices, progressive enhancement and zero hacks. Also, the first option will not be selectable (it's disabled) so the usability on **all platforms** is safeguarded as well – Aurelio Aug 16 '17 at 11:46
  • Is possible can I add color, so that customer can select color instead of text choices like,, colors red, blue color instead of one, two values,, – Gem Nov 16 '17 at 03:58
63

Here is the solution:

<select>
    <option style="display:none;" selected>Select language</option>
    <option>Option 1</option>
    <option>Option 2</option>
</select>
henrycjc
  • 663
  • 7
  • 20
Prabhu
  • 665
  • 5
  • 3
  • 5
    Note that `selected="true"` is invalid. Instead, use `selected="selected"` in (X)HTML or just `selected` in HTML. It seems you confused `selected` attribute with `selected` property, which is changed like this: `myOption.selected = true;` or `myOption.selected = false;` – Oriol Jan 20 '14 at 01:34
  • 2
    "display: none" is ignored by browsers that use device-native selects like Android and iOS. You must remove the option from DOM in select.focus and return it and select it in select.blur to make it work. – Radek Pech Mar 05 '14 at 08:50
  • -1; like [Nobita's answer using `hidden`](http://stackoverflow.com/a/13492609/1709587) (which is [*"typically implemented in CSS"*](https://html.spec.whatwg.org/multipage/interaction.html#the-hidden-attribute) using `display: none` anyway), using `display: none` on an `option` doesn't work in Safari; the option still appears. – Mark Amery Jan 20 '17 at 22:47
55

The proper and semantic way is using a placeholder label option:

  • Add an option element as the first child of the select
  • Set value= "" to that option
  • Set the placeholder text as the content of that option
  • Add the required attribute to the select

This will force the user to select another option in order to be able to submit the form, and browsers should render the option as desired:

If a select element contains a placeholder label option, the user agent is expected to render that option in a manner that conveys that it is a label, rather than a valid option of the control.

However, most browsers will render it as a normal option. So we will have to do fix it manually, by adding the following to the option:

select > .placeholder {
  display: none;
}
<select required>
  <option class="placeholder" selected disabled value="">Select language</option>
  <option>Option 1</option>
  <option>Option 2</option>
</select>
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • It should be noted that even this (which is the most thorough attempt at solving this problem out of all the answers here) doesn't work on Safari, either on iOS or Mac. The option will still when the select is opened. – Mark Amery Jan 20 '17 at 22:57
  • @MarkAmery I suggest you do like it's proposed in @Nobita answer and simply add the `hidden` attribute to the placeholder option tag instead of `display: none` as it seems that most mobile browsers ignore the `display: none` CSS attribute and it's even more semantically correct in my opinion. – Gaboik1 Apr 30 '18 at 18:33
  • @Gaboik1 In the comments on that answer, I noted that `hidden` is generally implemented as `display: none` under the hood (and the spec explicitly suggests this as a "typical" implementation). As such, I'd be surprised if there were any browsers - mobile or otherwise - where `hidden` worked but `display: none` did not. Can you provide any example? – Mark Amery May 01 '18 at 10:59
22

Because you can't use assign placeholders for select tags, I don't believe that there is any way to do exactly what you're asking for with pure HTML/CSS. You can, however, do something like this:

<select>
  <option disabled="disabled">Select language</option>
  <option>Option 1</option>
</select>

"Select language" will show up in the dropdown, but once another option is selected it will not be possible to reselect it.

I hope that helps.

Kyle
  • 318
  • 1
  • 9
  • 2
    I found that the best is the combination of disabled and selected with style="display:none;". disabled is useful when you want to perform validation - it makes sure you have no enabled option selected by default. – Illarion Kovalchuk Apr 08 '15 at 18:25
  • -1 because other answers here (notably Nobtia's and Oriol's) provide both much more explanatory detail and much more complete solutions. – Mark Amery Jan 20 '17 at 23:31
7

Try this:

<div class="selectSelection">
    <select>
        <option>Do not display</option>
        <option>1</option>
        <option>1</option>
    </select>
</div>

In the CSS:

.selectSelection option:first-child{
    display:none;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Aakash
  • 87
  • 1
  • 1
  • 1
    This answer contains nothing that other answers don't also contain in more detail, plus it has bad formatting; -1. – Mark Amery Jan 20 '17 at 23:41
5

I have a solution with a span displayed above the select until a selection done. The span displays the default message, and so it's not in the list of propositions:

HTML:

<span id="default_message_overlay">Default message</span>
<select id="my_select">
    <option value="1">Option 1</option>  
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
</select>

CSS:

#default_message_overlay {
    position: absolute;
    display: block;
    width: 120px;
    color: grey;
}
select {
    width: 150px;
}

Javascript (with JQuery):

$(document).ready(function() {
    // No selection at start
    $('#my_select').prop("selectedIndex", -1);

    // Set the position of the overlay
    var offset = $('#my_select').offset();
    offset.top += 3;
    offset.left += 3;
    $('#default_message_overlay').offset(offset);

    // Remove the overlay when selection changes
    $('#my_select').change(function() {
        if ($(this).prop("selectedIndex") != -1) {
            $('#default_message_overlay').hide();
        }
    });
});

I've made a jsfiddle for demo. Tested with Firefox and IE8.

Baldrick
  • 23,882
  • 6
  • 74
  • 79
  • Chrome on OSX always shows the first option selected, but it seems to be OSX specific, because all solutions provided here lead to the first visible option being selected. – Gamadril Dec 11 '15 at 09:44
  • instead of `span` in there use `legend` and that would be perfect or `label` – karlcow Aug 25 '16 at 05:59
  • This is an interesting approach, and the only possible way that I see of getting this behaviour to work on Safari, but it's badly implemented here; you at least need `pointer-events: none` on your span to allow clicks to pass through to select. – Mark Amery Jan 20 '17 at 23:36
1

To answer your question directly use this code on the option you do not want it to appear in option list:

<option value="" hidden selected>Select Language</option>
Luhaib-j
  • 79
  • 7
0
<option value="" id="ddl" name="prop" style="display:none;" disabled selected>chose something </option>

you can of course move the css to a css file if you want, and put a script to catch the esc button to select the disabled again. Unlike the other similar answers I put value="", this is so if you send the value(s) of your select list with a form, it won't contain "chose something". In asp.net mvc 5 sent as json compiled with var obj = { prop:$('#ddl').val(),...}; and JSON.stringify(obj); the value of prop will be null.

Anders M.
  • 199
  • 2
  • 14
0

Op1:

$("#MySelectid option").each(function () {
        if ($(this).html() == "text to find") {
            $(this).attr("selected", "selected");
            return;
        }
});

Op2:

$('#MySelectid option')
.filter(function() { return $.trim( $(this).text() ) == 'text to find'; })​​​​​​​​.attr('selected','selected');​​​​​​​