Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Range-input with two Inputs can be dragged over each other

I am trying to create a range-input with a maximum and minimum input.
I habe achived this so far:

HTML

<div class="zimmer__price">
 <input type="range" min="200" max="500" step="50" value="50" id="lower">
 <input type="range" min="200" max="500" step="50" value="500" id="upper">
</div>

JS:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

upperSlider.oninput = () => {
  lowerVal = parseInt(lowerSlider.value, 10);
  upperVal = parseInt(upperSlider.value, 10);

  currentPriceRange[1] = upperVal;
  currentPriceRange[0] = lowerVal;
};

lowerSlider.oninput = () => {
  lowerVal = parseInt(lowerSlider.value, 10);
  upperVal = parseInt(upperSlider.value, 10);

  currentPriceRange[1] = upperVal;
  currentPriceRange[0] = lowerVal;
};

My problem is, that you can drag the minimum input over the maximum and vise versa. How can I fix this?

>Solution :

This should work. You need to check the value of the other input and subtract or add to the value.

HTML

<div class="zimmer__price">
 <input type="range" min="200" max="500" step="50" value="50" id="lower">
 <input type="range" min="200" max="500" step="50" value="500" id="upper">
</div>

JS:

upperSlider.oninput = () => {
  lowerVal = parseInt(lowerSlider.value, 10);
  upperVal = parseInt(upperSlider.value, 10);

  if (upperVal < lowerVal + 50) {
    lowerSlider.value = upperVal - 50;
  }
  currentPriceRange[1] = upperVal;
  currentPriceRange[0] = lowerVal;
};

lowerSlider.oninput = () => {
  lowerVal = parseInt(lowerSlider.value, 10);
  upperVal = parseInt(upperSlider.value, 10);

  if (lowerVal > upperVal - 50) {
    upperSlider.value = lowerVal + 50;
  }
  currentPriceRange[1] = upperVal;
  currentPriceRange[0] = lowerVal;
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading