I have a select box. The options have been styled with different colors via a CSS file that has been referenced. I want to be able to select an option and change the text co
You could do it like this.
jsFiddle
JS
var select = document.getElementById('mySelect');
select.onchange = function () {
select.className = this.options[this.selectedIndex].className;
}
CSS
.redText {
background-color:#F00;
}
.greenText {
background-color:#0F0;
}
.blueText {
background-color:#00F;
}
You could use option { background-color: #FFF; }
if you want the list to be white.
HTML
<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>
Since this is a select
it doesn't really make sense to use .yellowText
as none selected if that's what you were getting at as something must be selected.
CSS
select{
color:red;
}
HTML
<select id="sel" onclick="document.getElementById('sel').style.color='green';">
<option>Select Your Option</option>
<option value="">INDIA</option>
<option value="">USA</option>
</select>
The above code will change the colour of text on click of the select box.
and if you want every option different colour, give separate class or id to all options.
ONE COLOR CASE - CSS only
Just to register my experience, where I wanted to set only the color of the selected option to a specific one.
I first tried to set by css only the color of the selected option with no success.
Then, after trying some combinations, this has worked for me with SCSS:
select {
color: white; // color of the selected option
option {
color: black; // color of all the other options
}
}
Take a look at a working example with only CSS:
select {
color: yellow; // color of the selected option
}
select option {
color: black; // color of all the other options
}
<select id="mySelect">
<option value="apple" >Apple</option>
<option value="banana" >Banana</option>
<option value="grape" >Grape</option>
</select>
For different colors, depending on the selected option, you'll have to deal with js.
JQuery Code:
$('#mySelect').change(function () {
$('#mySelect').css("background", $("select option:selected").css("background-color"));
});
This will replace the select
's background-color
with selected option
's background-color
.
Here is an example fiddle.
<html>
<style>
.selectBox{
color:White;
}
.optionBox{
color:black;
}
</style>
<body>
<select class = "selectBox">
<option class = "optionBox">One</option>
<option class = "optionBox">Two</option>
<option class = "optionBox">Three</option>
</select>
Try this:
.greenText{ background-color:green; }
.blueText{ background-color:blue; }
.redText{ background-color:red; }
<select
onchange="this.className=this.options[this.selectedIndex].className"
class="greenText">
<option class="greenText" value="apple" >Apple</option>
<option class="redText" value="banana" >Banana</option>
<option class="blueText" value="grape" >Grape</option>
</select>