0

I'm trying to modify a how-to example which I got from W3Schools

The example is a range slider which display the value of the slider inside a <span> tag

What I would like to do is display the value inside an input field

<div class="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
  <p>Value: <span id="demo"></span></p>
</div>

<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value;

slider.oninput = function() {
  output.innerHTML = this.value;
}
</script>

Source: W3Schools range slider example

I would like to display the value inside an input field instead of the <span> tag so I have tried to modify the example:

<div class="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
  <input type="number" id="demo" name="fname" value="">

</div>

<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("demo").value = slider.value;
output.innerHTML = slider.value;

slider.oninput = function() {
  output.innerHTML = this.value;
}
</script>

but this doesn't work as it only display the initial value and does not update if I move the slider knob

1
  • Don't get the value immediately. Use output.value = this.value; in the handler. Commented Oct 8, 2022 at 16:08

2 Answers 2

2

You can store the element's reference in the output var & instead of innerHTML you could just use the value attribute.

Here's the updated code for your reference:

var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.value = slider.value;

slider.oninput = function() {
  output.value = this.value;
}
<div class="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
  <input type="number" id="demo" name="fname" value="">
</div>
  

Sign up to request clarification or add additional context in comments.

Comments

0

setting an onchange function on the range onchange="myfunction()"so this function will be called every time you change the slider.

inside the function setting demo.value to slider.value

var slider = document.getElementById("myRange");
var output = document.getElementById("demo");


function myfunction() {
  demo.value = slider.value
}
<div class="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange" onchange="myfunction()">
  <input type="number" id="demo" name="fname" value="">

</div>

4 Comments

Why did you decide to inline the JS?
why not? he is a beginner so its easier to understand. he can always learn about event listeners when he gets a little grasp on JS
The OP is already using a listener in the JS code - oninput - so it's clear they understand them.
ok you are great. Congratulations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.