I have a HTML5 range slider. i.e. <input class="range-slider" type="range" id="points" name="points" min="0" max="220">
I am showing, hiding, and changing some content based on the selected range slide values.. with Vanilla JS, I have all my functionality logic done, but I hate how much code it is. Is there a better, leaner, more elegant way to do this? The below achieves everything I want it to, it just seems like way too much code.
export function donationSlider() {
resetAll()
const slider = document.querySelector('.range-slider');
const initial = document.querySelector('.tog-box-5');
const defimg = document.querySelector('#level-img-6');
const voluntBtn = document.querySelector('#volunteer-btn');
const donateBtn = document.querySelector('#donate-btn');
if (!slider || !initial || !defimg || !voluntBtn || !donateBtn) return
initial.classList.remove("hide");
initial.classList.add("show");
defimg.classList.add("show");
slider.oninput = ({ target: { value } }) => {
if (+value <= 1) {
resetAll()
document.querySelector('.tog-box-0').classList.add("show");
document.querySelector('#level-img-1').classList.add("show");
voluntBtn.href="?0";
donateBtn.href="xyz.php";
} else if (+value <= 10) {
resetAll()
document.querySelector('.tog-box-1').classList.add("show");
document.querySelector('#level-img-2').classList.add("show");
voluntBtn.href="?10";
donateBtn.href="xyz.php";
} else if (+value <= 20) {
resetAll()
document.querySelector('.tog-box-2').classList.add("show");
document.querySelector('#level-img-3').classList.add("show");
voluntBtn.href="?20";
donateBtn.href="xyz.php";
} else if (+value <= 35) {
resetAll()
document.querySelector('.tog-box-3').classList.add("show");
document.querySelector('#level-img-4').classList.add("show");
voluntBtn.href="?35";
donateBtn.href="xyz.php";
} else if (+value <= 50) {
resetAll()
document.querySelector('.tog-box-4').classList.add("show");
document.querySelector('#level-img-5').classList.add("show");
voluntBtn.href="?50";
donateBtn.href="xyz.php";
} else if (+value <= 100) {
resetAll()
initial.classList.add("show");
defimg.classList.add("show");
voluntBtn.href="?100";
donateBtn.href="xyz.php";
} else if (+value <= 200) {
resetAll()
document.querySelector('.tog-box-6').classList.add("show");
document.querySelector('#level-img-7').classList.add("show");
voluntBtn.href="?200";
donateBtn.href="xyz.php";
} else if (+value <= 1600) {
resetAll()
document.querySelector('.tog-box-7').classList.add("show");
document.querySelector('#level-img-8').classList.add("show");
voluntBtn.href="?1600";
donateBtn.href="xyz.php";
}
}
function resetAll() {
let togs = document.querySelectorAll('[class*="tog-box"]');
for (var i = 0; i < togs.length; ++i) {
togs[i].classList.add('hide');
togs[i].classList.remove('show');
}
let imgz = document.querySelectorAll('.level-imgs');
for (var i = 0; i < imgz.length; ++i) {
imgz[i].classList.add('hide');
imgz[i].classList.remove('show');
}
}
}
.hide
class, and when you want to show it, you could just remove it, instead of having an additional.show
class. \$\endgroup\$