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. Ofcourse 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;
}
Edit: 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;
}
I just added hidden attribute in option like below, It is working fine for me.
Here's my contribution. HAML + Coffeescript + SCSS
HAML
Coffeescript
SCSS
It's possible to use only CSS by changing the server code and only setting the class styles depending on the current value of the property, but this way seems easier and cleaner.
You can add more CSS (
select option:first-child
) to keep the placeholder gray when it opens, but I didn't care about that.Here is a CSS solution that works beautifully. The content is added (and absolutely positioned relative to the container) after the containing element (via :after pseudo-class). It gets its text from the placeholder attribute that I defined where I used the directive (attr(placeholder)). Another key factor is pointer-events: none - this allows clicks on the placeholder text to pass through to the select. Otherwise it won't drop down if the user clicks the text.
I add the .empty class myself in my select directive but normally I find that angular adds/removes .ng-empty for me (I assume it's b/c I'm injecting version 1.2 of Angular in my code sample)
(The sample also demonstrates how to wrap HTML elements in angularJS to create your own custom inputs)
If you are using angular go like this
For a required field, there is a pure-CSS solution in modern browsers:
User should not see the placeholder in select options. I suggest to use
hidden
attribute for placeholder option and you don't needselected
attribute for this option you can just put it as the first.