Change selected item color - select > option

Asked

Viewed 5,624 times

7

I got the following select.

inserir a descrição da imagem aqui

It is possible to change his blue color to one I want to apply, red for example?

1 answer

8


Elements SELECT are rendered by the operating system, not HTML. You cannot change this blue color because it is the default OS.

"The alternative is to use libraries that customize select, usually turned into dropdowns and others (Example: bootstrap select - silviomoreto.github.io/bootstrap-select)."

But Voce can change some things without using dropdowns:

var select = document.getElementById('mySelect');
select.onchange = function () {
    select.className = this.options[this.selectedIndex].className;
}
.redText {
    background-color:#F00;
}
.greenText {
    background-color:#0F0;
}
.blueText {
    background-color:#00F;
}
<select id="mySelect" class="greenText">
    <option class="greenText" value="apple" >Apple</option>
    <option class="redText"   value="banana" >Banana</option>
    <option class="blueText" value="grape" >Grape</option>
</select>

  • The alternative is to use libraries that customize the select, generally transformed them into dropdowns and others (Example: bootstrap select - http://silviomoreto.github.io/bootstrap-select/).

  • Eric, take a look at the example I put, and look at some of the changes that Voce can make.

  • But, note that the BLUE continues, because it is the standard of the Operating System.

  • 1

    Got it, Paulo. Thanks for the clarification.

  • And thank you, Eduardo, for the suggestion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.