9

WHAT:

I have an HTML5 range input slider. If I use jQuery to change the value of said slider, the handle's position does not change. This can be achieved using the .slider() method in the jQuery UI framework. However, I want to achieve the same result using the jQuery library.

QUESTION:

How do I dynamically set the value of an HTML 5 range input and have the handle move position using jQuery 1.11.2 or Javascript.

CODE ATTEMPTS:

Below is 2 snippets of code that I've used to try solve the problem.

$("#rangeInput").attr("value", "100");

$("#rangeInput").val("100")

THE FORM:

    <form id="noisePolForm">
<input id="rangeInput" type="range" value="0" step="3" min="80" max="140" class="noisePol">
    </form>
1
  • Note the second one should be $("#rangeInput").val("100"). Commented Oct 7, 2015 at 3:31

1 Answer 1

7

You need to use .prop(), not .attr():

$("#rangeInput").prop("value", "100"); //Or $("#rangeInput").prop("value", 100);

jsFiddle example

Your second example works fine, you just forgot the #

$("#rangeInput").val("100") //Or $("#rangeInput").val(100)

jsFiddle example

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

3 Comments

The OPs code works in older version of jQuery. Note this is because "As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values while .attr() retrieves attributes.".
@SpencerWieczorek - after jQuery 1.6 .prop() supplanted .attr() for most of what attr() was being used for.
Great to know to use .prop() instead of .attr() now. Thanks!

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.