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

158
Visualizações
Text input does not change slider value on first attempt

I am making a password generator with a text input and slider value that are tied together, however when I type in a value the first time it will not update. When I try again it works, as well as working subsequent times. The slider value always updates. I don't know what is wrong with the code to make it perform like this though, still fairly new to JS.

//html

<div class="slider">
    <input type="text" id="slider-input" value="" onchange="setSlider(this)">
    <input type="range" min="1" max="64" value="1" id="slider" oninput="myFunction(this)">
</div>

//JS

const sliderInput = document.getElementById("slider-input");
const slider = document.getElementById("slider");

let passLength = "";
function myFunction() {
    slider.oninput = function() {
    sliderInput.value = this.value;
    passLength = this.value;
    }
}

function setSlider() {
    sliderInput.addEventListener("change", function() {
    slider.value = this.value; 
    passLength = this.value;
    })
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Take a look at the setSlider function. On the first invocation you are adding the event listener. Therefore, it is only after that initial call that we see the intended behavior.

Rather than calling addEventListener on each change, you can remove it all together:

function setSlider({value}) {
  slider.value = value; 
  passLength = value;
}

repl

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