I have a simple dual slider as seen below.
The left slider can not push the right slider any more to the right as intended, but the right slider pushes the left slider to the left and I don’t know how to prevent this.
What am I missing?
// Elements for the sliders and display
const minSlider = document.getElementById("minL");
const maxSlider = document.getElementById("maxL");
const rangeDisplay = document.getElementById("rangeDisplay");
// Update the display and enforce slider constraints
function updateSliders() {
let minL = parseInt(minSlider.value);
let maxL = parseInt(maxSlider.value);
// Prevent the minSlider from exceeding maxSlider's value
if (minL > maxL) {
minSlider.value = maxL;
minL = maxL;
}
// Update the displayed range
rangeDisplay.textContent = `${minL} - ${maxL}`;
}
// Attach input event listeners for live updating
minSlider.addEventListener("input", updateSliders);
maxSlider.addEventListener("input", updateSliders);
// Initial display update
updateSliders();
.slider-container {
position: relative;
width: 100%;
max-width: 400px;
height: 20px;
}
#minL, #maxL {
position: absolute;
width: 100%;
pointer-events: none;
}
#minL::-webkit-slider-thumb, #maxL::-webkit-slider-thumb {
pointer-events: auto;
height: 18px;
width: 18px;
background-color: #333;
cursor: pointer;
}
<div id="controls">
<label>Range: <span id="rangeDisplay">0 - 70</span></label>
<div class="slider-container">
<input type="range" id="minL" min="0" max="360" value="0" step="1">
<input type="range" id="maxL" min="0" max="360" value="70" step="1">
</div>
</div>
</body>
</html>
3
Answers
You have the code for min > max
But I don’t see any check for the other way around?
To do the opposite you need to be checking for when the max slider is less than the min slider and adjust the max slider to equal the min slider in that case. Try adding this code.
I’m pretty sure your problem is because the minSlider can’t be more than the max slider so if the max slider goes under the min slider than the min slider is moving to the left to be less than it.
Edit: I think the order of operations matters too because whichever one goes first is going to get the priority. I’m guessing the real solution is probably to check which slider is being adjusted at the moment and prioritize the other one.
You need to adapt the logic based on which slider you are currently updating
Right now your code just sets the value of
minSlider
to the value ofmaxSlider
, but this only works if you changeminSlider
, so you need to do the opposite job when you changemaxSlider
.That’s why you need to identify which slider is changing and then set the right value for that slider.
You can not just apply the "opposite" condition for the other direction here – you need to first of all take into account, which of the sliders you are currently dragging.
One simple way to do that in this instance, would be to check the ID of the event target, whether that is
minL
ormaxL
.function updateSliders(e)
– I added thee
parameter here, so that your function takes theevent
object instance as parameter.And since you are also calling
updateSliders();
for the initial display, where no Event instance will get passed, I a also checking whethere
even exists, before trying to access itstarget
.