I want to make a battery. It only turns divs
into red and orange, but when it should turn green it doesn't work. I think that only the first two if
( if (x.value <= 30)) and else if
(else if (30 < x.value <= 50)) statements are working, others are just not. When I switch, and I put other else if
statements I the first else if
statement's place, it works. What can I do to solve this problem?
function changeColor() {
var a = document.getElementsByClassName("battery");
var x = document.getElementById("range");
if (x.value <= 30) {
a[3].style.backgroundColor = "#ff0000";
a[2].style.backgroundColor = "";
a[1].style.backgroundColor = "";
a[0].style.backgroundColor = "";
} else if (30 < x.value <= 50) {
a[3].style.backgroundColor = "#ff6600";
a[2].style.backgroundColor = "#ff6600";
a[1].style.backgroundColor = "";
a[0].style.backgroundColor = "";
} else if (50 < x.value <= 80) {
a[3].style.backgroundColor = "#00cc00";
a[2].style.backgroundColor = "#00cc00";
a[1].style.backgroundColor = "#00cc00";
a[0].style.backgroundColor = "";
} else if (x.value === 0) {
a.style.backgroundColor = "";
} else {
a[3].style.backgroundColor = "#009900";
a[2].style.backgroundColor = "#009900";
a[1].style.backgroundColor = "#009900";
a[0].style.backgroundColor = "#009900";
}
}
var slider = document.getElementById("range");
var y = document.getElementById("out");
y.innerHTML = slider.value;
slider.oninput = function() {
y.innerHTML = this.value + "%";
}
.battery {
width: 50px;
height: 20px;
margin-bottom: 2px;
margin-top: 2px;
margin-left: 3px;
margin-right: 3px;
}
#out {
width: 130px;
height: 20px;
text-align: center;
margin-top: 15px;
}
.allbattery {
border: 2px solid black;
width: fit-content;
}
<div class="main">
<div class="allbattery">
<div class="battery"></div>
<div class="battery"></div>
<div class="battery"></div>
<div class="battery"></div>
</div>
<div id="out"></div>
<input type="range" name="" id="range" onchange="changeColor()" value="0">
</div>