Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a placeholder for a 'select' box?

I'm using placeholders for text inputs which is working out just fine. But I'd like to use a placeholder for my selectboxes as well. Of course I can just use this code:

<select>     <option value="">Select your option</option>     <option value="hurr">Durr</option> </select> 

But the 'Select your option' is in black instead of lightgrey. So my solution could possibly be CSS-based. jQuery is fine too.

This only makes the option grey in the dropdown (so after clicking the arrow):

option:first {     color: #999; } 

The question is: How do people create placeholders in selectboxes? But it has already been answered, cheers.

And using this results in the selected value always being grey (even after selecting a real option):

select {     color: #999; } 
like image 260
Thomas Avatar asked Apr 27 '11 13:04

Thomas


People also ask

How do I create a select placeholder?

Answer: Use the disabled and selected Attribute There is no attribute like input's placeholder for select box dropdown. However, you can create similar effect by using the HTML disabled and selected attribute on a <option> element that has empty value.

Is there placeholder for select?

There does not exist a placeholder attribute for the <select> tag. However, there are some ways of making a placeholder for the select box. The easiest way of making a placeholder for the <select> element is using the disabled and selected attributes with the HTML5 hidden global attribute.

Can we add placeholder in select tag?

There is no placeholder attribute in 'select' tag but it can make the placeholder for a 'select' box. There are many ways to create the placeholder for a 'select' box.

How do you add a placeholder in Select 2?

Default selection placeholders$('select'). select2({ placeholder: { id: '-1', // the value of the option text: 'Select an option' } }); This is useful, for example, when you are using a framework that creates its own placeholder option.


2 Answers

A non-CSS - no JavaScript/jQuery answer:

<select>     <option value="" disabled selected>Select your option</option>     <option value="hurr">Durr</option> </select>

Update (December 2021):

This works for latest Firefox, Chrome, and Safari. It used to not work for many browsers in the past, as pointed out in the comments.

like image 135
David Avatar answered Oct 06 '22 06:10

David


I just stumbled across this question, and here's what works in Firefox and Chrome (at least):

<style>  select:invalid { color: gray; }  </style>  <form>  <select required>      <option value="" disabled selected hidden>Please Choose...</option>      <option value="0">Open when powered (most valves do this)</option>      <option value="1">Closed when powered, auto-opens when power is cut</option>  </select>  </form>

The Disabled option stops the <option> being selected with both mouse and keyboard, whereas just using 'display:none' allows the user to still select via the keyboard arrows. The 'display:none' style just makes the list box look 'nice'.

Note: Using an empty value attribute on the "placeholder" option allows validation (required attribute) to work around having the "placeholder", so if the option isn't changed, but is required, the browser should prompt the user to choose an option from the list.

Update (July 2015):

This method is confirmed working in the following browsers:

  • Google Chrome - v.43.0.2357.132
  • Mozilla Firefox - v.39.0
  • Safari - v.8.0.7 (the placeholder is visible in dropdown, but it is not selectable)
  • Microsoft Internet Explorer - v.11 (Placeholder is visible in dropdown but is not selectable)
  • Project Spartan - v.15.10130 (the placeholder is visible in dropdown, but it is not selectable)

Update (October 2015):

I removed the style="display: none" in favour of HTML5 attribute hidden which has wide support. The hidden element has similar traits as display: none in Safari, Internet Explorer, (Project Spartan needs checking) where the option is visible in dropdown, but it is not selectable.

Update (January 2016):

When the select element is required it allows use of the :invalid CSS pseudo-class which allows you to style the select element when in its "placeholder" state. :invalid works here because of the empty value in the placeholder option.

Once a value has been set, the :invalid pseudo-class will be dropped. You can optionally also use :valid if you so wish.

Most browsers support this pseudo-class. Internet Explorer 10 and later. This works best with custom styled select elements; in some cases i.e. (Mac in Chrome / Safari) you'll need to change the default appearance of the select box so that certain styles display, i.e., background-color and color. You can find some examples and more about compatibility at developer.mozilla.org.

Native element appearance Mac in Chrome:

Select box native Mac in Chrome

Using altered border element Mac in Chrome:

Altered select box Mac in Chrome

like image 26
William Isted Avatar answered Oct 06 '22 05:10

William Isted