3

How can you get the range slider to store output as a variable (which will then be set as a a session)

Thanks for the help

*have removed < and > so could display it with code

Below is my code (which doesn't work)

HTML Code:

    input type="range" min="1" max="100" value="50" class="slider" id="range"
    Value: span id="value" /span
    Value: span id="outputVar" /span

JavaScript Code:

    var slider = document.getElementById("range");
    var output = document.getElementById("value");
    var outputVarNo = document.getElementById("outputVar");
    output.innerHTML = slider.value;
2
  • 1
    Why do you believe this does not work? Do you see an error? Commented May 1, 2019 at 18:20
  • 2
    Code blocks allow entering full HTML. Please edit your code to reflect the actual HTML Commented May 1, 2019 at 18:23

2 Answers 2

4

Your example 'works' in that it does display the range value as you've described. It doesn't update the value because your JS code will execute only once at startup. To execute your code and update the output whenever the range input is changed, use an event listener:

  var slider = document.getElementById("range");
  var output = document.getElementById("value");
  var outputVarNo = document.getElementById("outputVar");

  let update = () => output.innerHTML = slider.value;

  slider.addEventListener('input', update);
  update();
<input type="range" min="1" max="100" value="50" class="slider" id="range">
Value: <span id="value"></span>
Value: <span id="outputVar"></span>

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

Comments

2

You may want to add an onChange event to your slider.

var slider = document.getElementById("range");
slider.onchange = function(event){
  var output = document.getElementById("outputVar");
  output.innerHTML = slider.value;
}

The answer provided by @junvar is better for live updates. Plus, the addEventListener is better IMO than directly using oninput

Comments

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.