Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of option selected - jQuery

I want to change the text color of the option that is selected="selected":

    <select class="select">
        <option value="--">--</option>
        <option value="2014">2014</option>
        <option value="2013">2013</option>
        <option value="2012" selected="selected">2012</option>
        <option value="2011">2011</option>
    </select>

I have been trying with CSS but it seems its not possible:

    .select select [selected="selected"]{
        color: #2b2b2b;
    }

Any ideas with jQuery?

like image 381
codek Avatar asked Dec 09 '22 10:12

codek


1 Answers

I have been trying with CSS but it seems its not possible:

Because you are targetting the select tag and not option tag also, that selector means select any select element nested inside element having .select class

select option[selected] {
    color: red;
}

Demo

You are using class so you can make a selector like

.select option[selected] {
   color: red;
}
like image 70
Mr. Alien Avatar answered Dec 21 '22 20:12

Mr. Alien