Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
Change value of the input range to the defined area corresponding to the min and max values

My input slider works fine upon onchange except 1 small problem is after I click on the button. The min and max values are fine. But the actual value like if it's 570 it should be closer to the right side because max will now be 600 and min will now be 400. It should only be centered if the value is 500.

I know I can change the value using elm.value = integer;

Thus why isn't the value of the range slider changing to the defined area corresponding to the min and max values?

var txt = document.getElementById('txt');

txt.onchange = function() {
  $val = this.value;

  if (this.value === this.max) {
    this.max = parseFloat(parseFloat(100) + parseFloat($val));
    this.min = parseFloat(parseFloat(-100) + parseFloat($val));
  } else if (this.value === this.min) {
    this.max = parseFloat(parseFloat(100) + parseFloat($val));
    this.min = parseFloat(parseFloat(-100) + parseFloat($val));
  }

  output.textContent = this.value;
};
txt.onchange();

key.onclick = function() {
  val = '570';

  // round integer to nearest 100
  valR = Math.round(val / 100) * 100;

  // if value is greater than 0
  txt.max = parseFloat(valR);
  txt.min = parseFloat(parseFloat(-100) + parseFloat(valR));

  this.value = val;
  txt.onchange();
};
<input id="txt" type="range" min="-100" max="100" value="0">
<span id="output"></span>

<button id="key">Get Data</button>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

lol the problem was I was calling this.value on my button's onclick function. I needed to declare the input element. Easy fix!

var txt = document.getElementById('txt');

txt.onchange = function() {
  $val = this.value;

  if (this.value === this.max) {
    this.max = parseFloat(parseFloat(100) + parseFloat($val));
    this.min = parseFloat(parseFloat(-100) + parseFloat($val));
  } else if (this.value === this.min) {
    this.max = parseFloat(parseFloat(100) + parseFloat($val));
    this.min = parseFloat(parseFloat(-100) + parseFloat($val));
  }

  output.textContent = this.value;
};
txt.onchange();

key.onclick = function() {
  val = '570';

  // round integer to nearest 100
  valR = Math.round(val / 100) * 100;

  // if value is greater than 0
  txt.max = parseFloat(valR);
  txt.min = parseFloat(parseFloat(-100) + parseFloat(valR));

  txt.value = val;
  txt.onchange();
};
<input id="txt" type="range" min="-100" max="100" value="0">
<span id="output"></span>

<button id="key">Get Data</button>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda