Question

I'm trying to make a box that has a class depending on the value inside the box.

  • if value is greater less then or equal to 3.3 then class = low
  • if value is less then 6.6 but higher then 3.3 then class = mid
  • if value is higher then 6.6 then class = high

below is the code im trying to use

  if ($(".total-score h3").text() <= "3.3") {
    $(".total-score").addClass('low');
}
else if ($(".total-score h3").text() > "3.3" < "6.6") {
    $(".total-score").addClass('mi');
}
else if ($(".total-score h3").text() > "6.6") {
    $(".total-score").addClass('high');
}
Was it helpful?

Solution

Create a variable to hold CLASS name. Process input using IF statement and assign appropriate name to CLASS variable Echo the class name back.

$returnedValue = 2.5; //The value being evaluated
$minRangeValue = 3.3; //Minimum Range
$maxRangeValue = 6.6; //Maximum Range
$boxClass = ""; //To hold the class name
if($returnedValue <= $minRangeValue){
    $boxClass = "low";
}
else if($returnedValue > $minRangeValue && $returnedValue <= $maxRangeValue){
    $boxClass = "mid";
}
else if($returnedValue > $maxRangeValue){
    $boxClass = "high";
}

echo '<div class="'. $boxClass .'"> Contenct </div>';

Note: Your logic was missing case for when value is exactly 6.6 I have assumed it is class=mid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top