14

Possible Duplicate:
<select> placeholder

I want to do something like this:

<select>
    <option selected="selected">Choose option</option>
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
    <option>Option 4</option>
</select>

here's the jsfiddle.

now I want the first option the selected one with the content "Choose option" to not be visible in the dropdown. how can i do that, if that's possible with a select element or should i do a custom thing. Thanks in advance.

Community
  • 1
  • 1
Yahya KACEM
  • 2,481
  • 7
  • 34
  • 61
  • 1
    I'm sorry, if you don't wnt it to appear, why not just remove it? – Jesus Zamora Jan 12 '13 at 23:26
  • because i want it to be there at the first load of the page i don't want to select the first option by default. – Yahya KACEM Jan 12 '13 at 23:27
  • @TillHelgeHelwig, and you think that question is the same here, because?!?! – Yahya KACEM Jan 12 '13 at 23:36
  • 1
    As far as I am aware selected="selected" is not completely valid. Since the selected attribute on its own is a boolean statement in an option statement, it’s presence alone states true, or absence false. Here’s the w3c’s example page based on the spec: http://www.w3.org/wiki/HTML/Elements/option – Mike Kormendy Oct 22 '14 at 13:17

3 Answers3

18

I believe the closest you can get with a plain HTML-solution is to disable that option:

<select>
    <option selected="selected" disabled="disabled">Choose option</option>
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
    <option>Option 4</option>
</select>

Demo

The element will still appear in the the list, but it will be grayed out and it will not be possible to select it from the dropdown after opening it.

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
7

You could remove the selected element as soon as you open the list:

$('select').click(function () {
  $('option[selected="selected"]', this).remove();
});

I am not aware of any native HTML/HTML5 solution.

philipproplesch
  • 2,127
  • 17
  • 20
5
<select>
    <option selected="selected" disabled="disabled">Choose option</option>
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
    <option>Option 4</option>
</select>

This will show "choose option" before selecting, but will not allow the user to select it.

Roy
  • 1,307
  • 1
  • 13
  • 29