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

116
Views
Change image SRC when input slider changed

I'm have a slider that goes from 1 - 3, I'd like my image src to change depending on what the slider is set to. I've tried to do this using an else if statement but it doesn't seem to be changing?

HTML

<span id="valBox"></span>
<input type="range" min="1" max="3" step="1">
<img id="demo" src="default-image.png">

Javascript

<script>
    if (step === 1) {
    document.querySelector("input.step").innerHTML.src = "one-image.png";
    } else if (step === 2) {
    document.querySelector("input.step").innerHTML.src = "two-image.png";
    }  else if (step === 3) {
    document.querySelector("input.step").innerHTML.src = "three-image.png";
    };
</script
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to run your JavaScript in an onchange event handler.

const imageElement = document.getElementById("demo");
const selectorElement = document.getElementById("selector");
selectorElement.onchange = () => {
  const step = selectorElement.valueAsNumber;  
  if (step === 1) {
      imageElement.src = "one-image.png";
  } else if (step === 2) {
      imageElement.src = "two-image.png";
  }  else if (step === 3) {
      imageElement.src = "three-image.png";
  }
}
<span id="valBox"></span>
<input id="selector" type="range" min="1" max="3" step="1">
<img id="demo" src="default-image.png">

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!