3

I found this code on stackoverflow but I can't change it to change the default values, for example I would like it to start from 20 instead of 0 and end at 80 instead of 100 when the page loads. I tried to change value and max in html but it didn't work. Could anyone help me? Thanks jsfiddle

    <div slider id="slider-distance">
<div>
<div inverse-left style="width:70%;"></div>
<div inverse-right style="width:70%;"></div>
<div range style="left:0%;right:0%;"></div>
<span thumb style="left:0%;"></span>
<span thumb style="left:100%;"></span>
<div sign style="left:0%;">
  <span id="value">0</span>
</div>
<div sign style="left:100%;">
  <span id="value">100</span>
</div>
</div>

<input type="range" name="info[age1]" value="0" max="100" min="0" step="1" oninput="
this.value=Math.min(this.value,this.parentNode.childNodes[5].value-1);
let value = (this.value/parseInt(this.max))*100
var children = this.parentNode.childNodes[1].childNodes;
children[1].style.width=value+'%';
children[5].style.left=value+'%';
children[7].style.left=value+'%';children[11].style.left=value+'%';
children[11].childNodes[1].innerHTML=this.value;" />

<input type="range" name="info[age2]" value="100" max="100" min="0" step="1" oninput="
this.value=Math.max(this.value,this.parentNode.childNodes[3].value-(-1));
let value = (this.value/parseInt(this.max))*100
var children = this.parentNode.childNodes[1].childNodes;
children[3].style.width=(1`enter code here`00-value)+'%';
children[5].style.right=(100-value)+'%';
children[9].style.left=value+'%';children[13].style.left=value+'%';
children[13].childNodes[1].innerHTML=this.value;" />
</div>

EDIT: I solved it, thank you all!

<div slider id="slider-distance">
<div>
<div inverse-left style="width:70%;"></div>
<div inverse-right style="width:70%;"></div>
<div range style="left:20%;right:0%;"></div>
<span thumb style="left:20%;"></span>
<span thumb style="left:100%;"></span>
<div sign style="left:20%;">
  <span>20</span>
</div>
<div sign style="left:100%;">
  <span>100</span>
</div>
</div>

<input type="range" name="info[age1]" value="20" max="100" min="0" step="1" oninput="
this.value=Math.min(this.value,this.parentNode.childNodes[5].value-1);
let value = (this.value/parseInt(this.max))*100
var children = this.parentNode.childNodes[1].childNodes;
children[1].style.width=value+'%';
children[5].style.left=value+'%';
children[7].style.left=value+'%';children[11].style.left=value+'%';
children[11].childNodes[1].innerHTML=this.value;" />

<input type="range" name="info[age2]" value="100" max="100" min="0" step="1" oninput="
this.value=Math.max(this.value,this.parentNode.childNodes[3].value-(-1));
let value = (this.value/parseInt(this.max))*100
var children = this.parentNode.childNodes[1].childNodes;
children[3].style.width=(100-value)+'%';
children[5].style.right=(100-value)+'%';
children[9].style.left=value+'%';children[13].style.left=value+'%';
children[13].childNodes[1].innerHTML=this.value;" />
</div>
Gae
  • 71
  • 1
  • 7

1 Answers1

1

The code is written for 0 - 100 values only. Simply changing the min and max won't help.

    <div sign style="left:0%;">
      <span id="value">20</span>
    </div>
    <div sign style="left:100%;">
      <span id="value">80</span>
    </div>
  </div>
  <input id="age1Slider" type="range" name="info[age1]" value="20" max="80" min="20" step="1" />
  <input id="age2Slider" type="range" name="info[age2]" value="80" max="80" min="20" step="1" />

I also extracted the code from the inline js to propper js.

document.getElementById('age1Slider').oninput = function age1(){
  this.value=Math.min(this.value,this.parentNode.childNodes[5].value-1);
  let value = ((this.value - parseInt(this.min))/(parseInt(this.max) - parseInt(this.min)))*100
  var children = this.parentNode.childNodes[1].childNodes;
  children[1].style.width=value+'%';
  children[5].style.left=value+'%';
  children[7].style.left=value+'%';
  children[11].style.left=value+'%';
  children[11].childNodes[1].innerHTML=this.value;
}

document.getElementById('age2Slider').oninput = function age2(){
  this.value=Math.max(this.value,this.parentNode.childNodes[3].value-(-1));
  let value = ((this.value - parseInt(this.min))/(parseInt(this.max) - parseInt(this.min)))*100
  var children = this.parentNode.childNodes[1].childNodes;
  children[3].style.width=(100-value)+'%';
  children[5].style.right=(100-value)+'%';
  children[9].style.left=value+'%';
  children[13].style.left=value+'%';
  children[13].childNodes[1].innerHTML=this.value;
}

The important change was let value = ((this.value - parseInt(this.min))/(parseInt(this.max) - parseInt(this.min)))*100.

A full Example you can see here https://jsfiddle.net/HackLab/p2tcwaub/

You still have to change max and min in HTML in 8 places!

HackLab
  • 509
  • 3
  • 10