JSFiddle - React, Tailwind, and code Playground
HTML
<div id="0">0000 - 1000</div>
<div id="1001">1001 - 2000</div>
<div id="2001">2001 - 3000</div>
<div id="3001">3001 - 4000</div>
<input type="text" id="txt"/>
<input type="button" id="btn" value="Press me">
JavaScript
function clearBackgrounds() {
for (let id of ["0", "1001", "2001", "3001"]) {
document.getElementById(id).style.backgroundColor = "white";
}
}
function someFunc(){
const val = document.getElementById("txt").value;
if (isNaN(Number(val))) {
alert("Wrong number!");
}
else if (val >= 0 && val <= 1000) {
alert("0000 - 1000 range!"); //why not 0001 - 1000 btw
clearBackgrounds();
document.getElementById("0").style.backgroundColor = "lavender";
}
else if (val >= 1001 && val <= 2000) {
alert("1001 - 2000 range!");
clearBackgrounds();
document.getElementById("1001").style.backgroundColor = "lavender";
}
else if (val >= 2001 && val <= 3000) {
alert("2001 - 3000 range!");
clearBackgrounds();
document.getElementById("2001").style.backgroundColor = "lavender";
}
else if (val >= 3001 && val <= 4000) {
alert("3001 - 4000 range!");
clearBackgrounds();
document.getElementById("3001").style.backgroundColor = "lavender";
}
else {
alert("Unknown range!");
}
}
document.getElementById("btn").onclick = someFunc;