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

194
Views
How to change slider output text on reset in JavaScript

I have code that looks something like below. When I hit the reset button, I want my paragraph element to update to the default slider values, but they stay at the same value from before the reset. How can I make it so that the paragraph element reflects the reset slider value?

JSFiddle Link: https://jsfiddle.net/apshah/f6ymkcLe/

HTML:

<div class ='slidercontainer' id='slidercontainer'>
  <form action method="POST" class="sliderform" id="sliderform">
  
  <div class = "divslider" id="slider1">
    <input type="range" min="1" max="100" value="50" class="slider" id="myRange1">
    <p>
      <span class="sVal" id="val1">50</span>
    </p>
  </div>
  <div class = "divslider" id="slider2">
    <input type="range" min="1" max="10" value="5" class="slider" id="myRange2">
    <p>
      <span class="sVal" id="val2">5</span>
    </p>
  </div>
  </form>
  <div class="space1"></div>
  <div class='buttonbox'>
    <button type="submit" form="sliderform" class="sliderbutton" id="accept">Accept</button>
    <button type="reset" form="sliderform" class="sliderbutton" id="reset">Reset</button>
    <div class="space2"></div>
  </div>
</div>

JavaScript:

document.body.onload = function(){
    runSlider("myRange1", 'val1')
  runSlider('myRange2', 'val2')
}

function runSlider(inputID, pID){
  var slider = document.getElementById(inputID);
  var output = document.getElementById(pID);
  output.innerHTML = slider.value;

  slider.oninput = function() {
    output.innerHTML = this.value;
  }

}
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You should add another listener for this. I used setTimeout because of async.

document.body.onload = function(){
  runSlider("myRange1", 'val1')
  runSlider('myRange2', 'val2')
  registerFormEvent()
}

function registerFormEvent() {
    const formEl = document.querySelector(".sliderform");
  
  const onFormReset = function(){
    setTimeout(() => {
      document.querySelector("#val1").innerHTML = document.querySelector("#myRange1").value;
        document.querySelector("#val2").innerHTML = document.querySelector("#myRange2").value;
    }, 0)
  }
  
  formEl.addEventListener("reset", onFormReset)
}

function runSlider(inputID, pID){
  var slider = document.getElementById(inputID);
  var output = document.getElementById(pID);
  output.innerHTML = slider.value;

  slider.oninput = function() {
    output.innerHTML = this.value;
  }

}
about 4 years ago · Santiago Gelvez 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!