I have scoring system where I want to change icon color by changing CSS classes.
How can I optimize this jQuery? After testing, I found this the only alternative, but I trust there is a more simplified method.
// Count scores
$('#bodyScore').text((90 / myarray.length) * bodyScore + '/90');
// Change icon color
var bodyPercent = bodyScore / myarray.length;
var successRatio = 0.9;
var warningRatio = 0.5;
if (bodyPercent >= successRatio) {
$(".bkf").removeClass("label-success");
$(".bkf").removeClass("label-warning");
$(".bkf").removeClass("label-important");
$(".bkf").addClass("label-success");
}
else if (bodyPercent >= warningRatio) {
$(".bkf").removeClass("label-success");
$(".bkf").removeClass("label-warning");
$(".bkf").removeClass("label-important");
$(".bkf").addClass("label-warning");
}
else {
$(".bkf").removeClass("label-success");
$(".bkf").removeClass("label-warning");
$(".bkf").removeClass("label-important");
$(".bkf").addClass("label-important");
}