I have a select element which has several items. I want to change the color of its first item. But it seems the color only shows when you click on the select dropdown. What I want is changed the color(like color gray) when the page is loaded so users can see the first option color changed.
See the example here... http://jsbin.com/acucan/4/
Thanks for the answer! It's the very first time I post question here. Sorry about my English. I hope that I make the question clear.
Thanks
If the first item is to be used as a placeholder (empty value) and your select is
required
then you can use the:invalid
pseudo-class to target it.You can do this by using CSS: JSFiddle
HTML:
CSS:
Or if you absolutely need to use JavaScript (not adviced for this): JSFiddle
JavaScript:
CSS:
I really wanted this (placeholders should look the same for text boxes as select boxes!) and straight CSS wasn't working in Chrome. Here is what I did:
First make sure your select tag has a
.has-prompt
class.Then initialize this class somewhere in
document.ready
.Then in CSS:
Here is a way so that when you select an option, it turns black. When you change it back to the placeholder, it turns back into the placeholder color (in this case red).
http://jsfiddle.net/wFP44/166/
It requires the options to have values.
What about this:
http://jsbin.com/acucan/9