Reputation: 780
I have this select input:
<select name="color_tarea" id="color_tarea" style="font-family: 'FontAwesome', Arial;" class="form-control">
<option value="danger" class="text-danger"> Máxima prioridade</option>
<option value="warning" class="text-warning"> Prioridade elevada</option>
<option value="info" class="text-info"> Información</option>
<option value="primary" class="text-primary"> Pouco importante</option>
<option value="success" class="text-success"> Cando se poda</option>
</select>
Every option has his own text color. Now I want to change the main color (selected option) with the color selected. I can't find a way with only CSS, how can I get the class of the selected option for changing it with jQuery?
Thanks in advance.
EDIT: The question marked as possible solution is not my solution. That is for changing the placeholder (text of an input when is no text written). I need the text on the main select box (not an option) changes with the color of the chosen option. I wonder this explains correctly the situation.
Upvotes: 2
Views: 3602
Reputation: 5494
It's not very easy with CSS only. Here are some options:
option:checked {
color:lightgreen;
}
<select name="color_tarea" id="color_tarea" style="font-family: 'FontAwesome', Arial;" class="form-control">
<option value="danger" class="text-danger"> Máxima prioridade</option>
<option value="warning" class="text-warning"> Prioridade elevada</option>
<option value="info" class="text-info"> Información</option>
<option value="primary" class="text-primary"> Pouco importante</option>
<option value="success" class="text-success"> Cando se poda</option>
</select>
unfortunately I haven't managed to make it work with CSS only, and it only works on size="2"
or higher (i.e. not 1). This runs on jQuery:
$(document).ready(function (event) {
$('select').on('mouseenter', 'option', function (e) {
this.style.color = "limegreen";
});
$('select').on('mouseleave', 'option', function (e) {
this.style.color = "black";
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select size="5" name="color_tarea" id="color_tarea" style="font-family: 'FontAwesome', Arial;" class="form-control">
<option value="danger" class="text-danger"> Máxima prioridade</option>
<option value="warning" class="text-warning"> Prioridade elevada</option>
<option value="info" class="text-info"> Información</option>
<option value="primary" class="text-primary"> Pouco importante</option>
<option value="success" class="text-success"> Cando se poda</option>
</select>
Alas, the CSS only solution doesn't work when you make the size
attribute higher than 1.
You can use a library like this one. Or you could build your own dropdown with a good old ul>li setup.
Based my answer on this and some research. That's as much as I can do, best of luck to you.
Upvotes: 1