I have the following select:
<select class="state">
<option value="1">Done</option>
<option value="2">Closed</option>
<option value="2">Open</option>
<option value="2">Working on it</option>
<option value="2">Waiting</option>
</select>
The following script is changing the color depending of its content:
$('.state option:selected').each(function(){
var state = $(this).text();
if(state == 'Open') {
$(this).parent().parent().css('background-color', '#e74c3c');
} else if(state == 'Working on it') {
$(this).parent().parent().css('background-color', '#f1c40f');
} else if(state == 'Closed') {
$(this).parent().parent().css('background-color', '#c0392b');
} else if(state == 'Waiting') {
$(this).parent().parent().css('background-color', '#d35400');
} else if(state == 'Done') {
$(this).parent().parent().css('background-color', '#27ae60');
} else {
$(this).parent().parent().css('background-color', '#bdc3c7');
}
});
$('select').change(
function (){
var color = $('option:selected',this).css('background-color');
$(this).css('background-color',color);
}
).change();
But what this script is not doing: It is not changing the color if I change the value of the select. Additionally I am sure, that this code could be much shorter.
The select has a class because this select will appear more than once on a single page.