I've got two bootstrap-select forms, that use the same JS. It's two lists of names of colours styled to appear in the colours described with CSS classes.
I've got the CSS class names also stored in each option element's value, so that it can be accessed by the JS, so it can remain in the right colour after and before clicked.
I'm trying to change the colour that way by adding and removing the classes with 'addClass' and 'removeClass', but removeClass isn't working. This means that you can click down through the various options and the option displayed will appear in the right colour, but when you go back and choose one previously clicked, it won't go back to that colour.
Can someone explain the problem?
As a JSFiddle
HTML
<div class="selectContainer">
<select class="form-control pickerSelectClass" id="select_1">
<option class="big _21" value="_21">■ Bright Red</option>
<option class="big _106" value="_106">■ Bright Orange</option>
<option class="big _24" value="_24">■ Bright Yellow</option>
</select>
<select class="form-control pickerSelectClass" id="select_2">
<option class="big _21" value="_21">■ Bright Red</option>
<option class="big _106" value="_106">■ Bright Orange</option>
<option class="big _24" value="_24">■ Bright Yellow</option>
</select>
</div>
CSS
.selectContainer {width:200px}
.big {
font-size: 16px;
font-weight: bold !important;
text-shadow: -1px -1px 0 #444, 1px -1px 0 #444, -1px 1px 0 #444, 1px 1px 0 #444;
}
._21 {
color: red !important
}
._106 {
color: orange !important
}
._24 {
color: yellow !important
}
JS
$(document).ready(function() {
$(".pickerSelectClass").selectpicker();
$('select').each(function(index, item){
$(this).parent().find('.filter-option').addClass("big");
$(this).parent().find('.filter-option').addClass($(this).val());
}).on('change', function(){
$(this).parent().find('.filter-option').removeClass('_*')
$(this).parent().find('.filter-option').addClass($(this).val());
});
});
External resources
jquery.min.js,
bootstrap.css,
bootstrap.min.js,
bootstrap-select.css,
bootstrap-select.min.js