Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

89
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda