I am creating a BMI index calculator so far everything hass been good and I am lost when i think about how I should be able to change the background color of the div class to certain color according to their BMI result.
for example:
- if the person is
underweightit should be:grey - if the person is
normalit should be:blue - if the person is
overweightit should be:orange - if the person is
obeseit should be:red
How do i do this please help...
Here is the HTML CSS and Javascript
<html>
<head>
<title>BMI Calculator</title>
<script type="text/javascript">
function computeBMI() {
//Obtain user inputs
var height = Number(document.getElementById("height").value);
var heightunits = document.getElementById("heightunits").value;
var weight = Number(document.getElementById("weight").value);
var weightunits = document.getElementById("weightunits").value;
if (heightunits == "inches") height /= 39.3700787;
if (weightunits == "lb") weight /= 2.20462;
var BMI = weight / Math.pow(height, 2);
document.getElementById("output").innerText = Math.round(BMI * 100) / 100;
var result;
if (BMI <= 18.5) {
result = 'underweight';
} else if (BMI <= 24.9) {
result = 'Normal';
} else if (BMI <= 29.9) {
result = 'Overweight';
} else {
result='Obese';
}
}
</script>
<style type="text/css">
.resultclass{ background-color:yellow; width:500px; height:300px;}
.underweight{ background-color:grey; width:500px; height:300px;}
.normal{ background-color:blue; width:500px; height:300px;}
.overweight{ background-color:orange; width:500px; height:300px;}
.obese{ background-color:red; width:500px; height:300px;}
</style>
</head>
<body>
<h1>Body Mass Index Calculator</h1>
<p>Enter your height: <input type="text" id="height"/>
<select type="multiple" id="heightunits">
<option value="metres" selected="selected">metres</option>
<option value="inches">inches</option>
</select>
</p>
<p>Enter your weight: <input type="text" id="weight"/>
<select type="multiple" id="weightunits">
<option value="kg" selected="selected">kilograms</option>
<option value="lb">pounds</option>
</select>
</p>
<input type="submit" value="computeBMI" onclick="computeBMI();">
<div class="resultclass">
<h1>Your BMI is: <span id="output">?</span></h1>
<h1>This means you are: <span id="resultdiv">?</span></h1>
</div>
</body>
</html>