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 had the same problem and while searching came across this question, and after I found good solution for me I would like to share it with you guys in case some one can benefit from it. here it is: HTML:
CSS:
JS:
After customer makes first select no need for gray color so JS removes the class
place_holder
. I hope this helps someone :)Update: Thanks to @user1096901, as a work around for IE browser, you can add
place_holder
class again in case first option is selected again :)In respect to all solutions above, but this one seems to be most valid due to HTML specs:UPDATE: Pardon me for this incorrect answer, this is definitely not a placeholder solution for the
select
element, but a title for grouped options under openedselect
element list.How about a non CSS - no javascript/jQuery answer?
There's no need for any JS or CSS, just 3 attributes:
it doesn't show the option at all, just sets the option's value as the default.
However, if you just don't like a placeholder that's the same color as the rest, you can fix it inline like this:
Obviously, you can separated the functions and at least the select's CSS into separate files.
Note: the onload function corrects a refresh bug.
I wanted the SELECT to be grey until selected so for this piece of HTML:
I've added these CSS definitions:
It works as expected in Chrome / Safari, maybe also in other browsers but I haven't checked.