1

I'm using js to get value from the range slider. now I wanted to show that in the input box.

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

slider.oninput = function() {
  output.innerHTML = this.value;
}
 <input type="range" min="1" max="10"  name="ans" class="slider" id="myRange">
 value:<p><span id="demo"></p>
 

NEED THE VALUE TO BE SHOWN IN THE INPUT BOX

Ahmad Karimi
  • 1,285
  • 2
  • 15
  • 26
teenage vampire
  • 339
  • 1
  • 5
  • 13

3 Answers3

2

You just need some minor changes.

var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
var inputItem = document.getElementById("outputPounds");
output.innerHTML = slider.value;
weightConverter(slider.value);
slider.oninput = function() {
  output.innerHTML = this.value;
  weightConverter(slider.value);
}

function weightConverter(valNum) {
  inputItem.value=valNum*2.2046;
}
<input type="range" min="1" max="200" name="ans" class="slider" 
 id="myRange">

 <p class="agecls">Weight:<span id="demo" style="text-decoration:underline" 
 oninput="weightConverter(this.value)" 
 onchange="weightConverter(this.value)"></span></p>

 <p>Pounds: <input type="text" value="0" id="outputPounds"></p>
Viral
  • 935
  • 1
  • 9
  • 22
  • still can't find the solution checkout my code on code pen -> https://codepen.io/ajitstark/pen/jONKeej?editors=1010#0 – teenage vampire Sep 13 '19 at 14:58
  • Please run the above code. I have updated it. Please let me know if you are still getting an issue. @teenagevampire – Viral Sep 17 '19 at 13:26
0

Please update the code by this

    <input type="range" min="1" max="200" name="ans" class="slider" id="myRange">
    <p class="agecls">Weight:<span id="demo" style="text-decoration:underline"></span></p>
    <p>Pounds: <span id="outputPounds"></span></p>

    <script type="text/javascript">

    var slider = document.getElementById("myRange");
    var output = document.getElementById("demo");
    output.innerHTML = slider.value;
    weightConverter(slider.value)
    slider.oninput = function() {
      output.innerHTML = this.value;
      weightConverter(this.value)
    }


    function weightConverter(valNum) {
      document.getElementById("outputPounds").innerHTML=valNum*2.2046;
    }
    </script>
ankit singh
  • 565
  • 3
  • 8
0

Your entire code could be simplified to a function which reads the input value and updates each of the two places where you want to display the value in either metric or imperial system.

Placing everything inside one function enables you to bind the execution of this function on two events:

a) on window.onload event (so the conversion and display are done when page loads);
b) on input.oninput event, so they are also done when you slide. Note that change event only fires once you release the slider while input event fires when a slider value input has been detected, irrespective of input method (keyboard, mouse, touch, etc...).

Working example:

const updateValues = function() {
  let inputValue = document.querySelector('#myRange').value;
  document.querySelector('#outputKg').innerHTML = inputValue;
  document.querySelector('#outputLbs').innerHTML = Math.round(inputValue * 220.462262) / 100;
}
window.onload = updateValues;
document.querySelector('#myRange').oninput = updateValues;
<input type="range" min="1" max="200" name="ans" class="slider" id="myRange">
<p>Weight: <span id="outputKg"></span></p>
<p>Pounds: <span id="outputLbs"></span></p>
tao
  • 82,996
  • 16
  • 114
  • 150