2

I'm using default HTML5 range slider. It works, but is it possible to change the value of this slider on the go (when mousebutton is clicked on range bar and mouse slides to any side)? Important: is it possible without using jQueryUI, but with jQuery (or pure JS) only?

HTML:

<input type="range" name="oc-max-price" id="oc-max-price" min="390" max="5000" step="10" value="5000">
<span id="oc-max-price-val">5000</span>

jQuery code:

$('#oc-max-price').change(function () {
    var mP = $('#oc-max-price').val();
    $('#oc-max-price-val').text(mP);
    console.log(mP);
});
2
  • try .on('input', .... Commented Feb 14, 2015 at 18:28
  • View the edited answer for pure JS solution. Commented Feb 14, 2015 at 18:39

3 Answers 3

3

Use oninput

   $(document).ready(function(){
       $('#oc-max-price').on("input", function () {
        var mP = $('#oc-max-price').val();
        $('#oc-max-price-val').text(mP);
        console.log(mP);
    }); 
    });

Pure JavaScript

var input = document.querySelector('#oc-max-price');

var messages = document.querySelector('#oc-max-price-val');

input.addEventListener('input', function()
{
    messages.textContent = input.value;
});
Sign up to request clarification or add additional context in comments.

3 Comments

mousemove does not trigger iPhone/iPad or any touch device sadly. So i guess input is anyway better.
Is it ok if I use bunch of them (input, touchmove and mousemove)? Or maybe it's "mauvais ton"?
Just look at the compatibility developer.mozilla.org/en-US/docs/Web/API/…. I dont think their should be any issue in firefox or IE as it is fully supported. The issue could be machine configuration/situation dependent.
1

Set an interval: http://jsfiddle.net/2ej2m3kp/

 $('#oc-max-price').mousedown(
    setInterval(function () {
        var mP = $('#oc-max-price').val();
        $('#oc-max-price-val').text(mP);
        console.log(mP);
    }, 100));

Comments

1

So, like on mousemove & touchmove?

$('#oc-max-price').on('mousemove touchmove', function () {
    var mP = $(this).val();
    $('#oc-max-price-val').text(mP);
});

Here's a fiddle.

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.