0

I would like some help with getting this range slider to give multiple outputs. I've tried a few different things but I can't seem to figure it out.

I would like the "590" text to be 5.9x the slider value with commas if the number reaches the thousands I would like the "30,000" text to be 300x the slider value also with commas

[JSFiddle Demo]

var elem = document.querySelector('input[type="range"]');

var rangeValue = function(){
  var newValue = elem.value;
  var target = document.querySelector('.value');
  target.innerHTML = newValue;
}

elem.addEventListener("input", rangeValue);
@import url(https://fonts.googleapis.com/css?family=Work+Sans:300);
body {
  font-family: "Work Sans", Helvetica, Arial, sans-serif; 
  color: #d7d7d7;
  padding-top: 40px;
  text-shadow: white 1px 1px 1px;
}
.value {
  text-align: center;
  font-weight: 300;
  font-size: 10em;
  width: 300px; 
  height: 100px;
  line-height: 60px;
  letter-spacing: -.03em;
  margin: 40px auto;
}
input[type="range"] {
  display: block;
  -webkit-appearance: none;
  background-color: #ffa842;
  width: 300px;
  height: 5px;
  border-radius: 5px;
  margin: 0 auto;
  outline: 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  background-color: #f5a623;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid white;
  cursor: pointer;
  transition: .3s ease-in-out;
}​
  input[type="range"]::-webkit-slider-thumb:hover {
    background-color: white;
    border: 2px solid #f5a623;
  }
  input[type="range"]::-webkit-slider-thumb:active {
    transform: scale(1.6);
  }
<div class="value">100</div>
<input type="range" min="100" max="1000" step="10" value="100">

<br><br>
<strong><span class="perYear" id="perYear" style=" font-size: 70px; font-weight: 700; color: #ff9600;">590</span></strong>
<br><br>
<strong><span class="perTwentyFive" id="perTwentyFive" style=" font-size: 70px; font-weight: 700; color: #ff9600;">30,000</span></strong>

1 Answers1

0

I added a few lines to the Javascript.

When the range value changes, you can calculate 5.9 * newValue. The insertion of commas I based on this answer. using parseInt() to round it in order to avoid weird numbers like 1003.0000000000001.

var elem = document.querySelector('input[type="range"]');

var rangeValue = function(){
  var newValue = elem.value;
  document.querySelector('.value').innerHTML = newValue;
  var perYear = insertCommas(5.9 * newValue);
  document.querySelector("#perYear").innerHTML = perYear;
  var perTwentyFive = insertCommas(300 * newValue);
  document.querySelector("#perTwentyFive").innerHTML = perTwentyFive;
}

elem.addEventListener("input", rangeValue);

function insertCommas(x) {
  return parseInt(x).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
@import url(https://fonts.googleapis.com/css?family=Work+Sans:300);
body {
  font-family: "Work Sans", Helvetica, Arial, sans-serif; 
  color: #d7d7d7;
  padding-top: 40px;
  text-shadow: white 1px 1px 1px;
}
.value {
  text-align: center;
  font-weight: 300;
  font-size: 10em;
  width: 300px; 
  height: 100px;
  line-height: 60px;
  letter-spacing: -.03em;
  margin: 40px auto;
}
input[type="range"] {
  display: block;
  -webkit-appearance: none;
  background-color: #ffa842;
  width: 300px;
  height: 5px;
  border-radius: 5px;
  margin: 0 auto;
  outline: 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  background-color: #f5a623;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid white;
  cursor: pointer;
  transition: .3s ease-in-out;
}​
  input[type="range"]::-webkit-slider-thumb:hover {
    background-color: white;
    border: 2px solid #f5a623;
  }
  input[type="range"]::-webkit-slider-thumb:active {
    transform: scale(1.6);
  }
<div class="value">100</div>
<input type="range" min="100" max="1000" step="10" value="100">

<br><br>
<strong><span class="perYear" id="perYear" style=" font-size: 70px; font-weight: 700; color: #ff9600;">590</span></strong>
<br><br>
<strong><span class="perTwentyFive" id="perTwentyFive" style=" font-size: 70px; font-weight: 700; color: #ff9600;">30,000</span></strong>
DenverCoder1
  • 2,253
  • 1
  • 10
  • 23
  • Thank you so much! It works perfectly. I'll learn from what you added. –  Jul 17 '19 at 11:20