Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!