33

Hey having a little trouble jquery and the hmtl5 range. I'm try to get the values as they changed but the event listener is not capturing it. Currently my code is:

HTML

html += '<li>Apply Credits: <input type="range"  min="0" max="'+credits+'" name="discount_credits" id="discount_credits" /> <span>'+credits+'</span></li>'

And the JS is:

$('#discount_credits').mousewheel( function() { 
        alert('it changed!'); 
        alert('new value = ' + $(this).val()); 
    });

I've also tried

$('#discount_credits').change( function() { 
            alert('it changed!'); 
            alert('new value = ' + $(this).val()); 
        });

Neither seem to work. Am I doing something wrong?

1
  • Is the event handler added after the range input is injected into the DOM? Commented May 6, 2012 at 19:36

5 Answers 5

49
$('input[type=range]').on('input', function () {
    $(this).trigger('change');
});

This fires the change event on every input event.

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

Comments

19

Does not seem to have any problem on HTML5 <input type="range"> using change.

See: http://jsfiddle.net/DerekL/BaEar/33/

3 Comments

It only updates the span value when I release the slider button, on keyup. In Google Chrome Version 34.0.1847.116 using jQuery 2.0.3.
may not be big of a deal, but it also triggers when just hovering the slider with the mouse. otherwise great solution!
in Chrome, at least, attaching handler to the 'input' event gets triggered even when hovering. for the 'change' event, this is not the case. so i opted for 'change', as hovering is not considered input in my case. the hovering effect could be problematic if your handler triggers dom manipulation or ajax calls.
6

I assume your span has an id: <span id="slidernumber">...</span> Lets also assume that you have a few sliders, and you want to see a text change if any of them were moved:

<li>
Apply Credits1: 
<input type="range" min="0" max="100" 
name="discount_credits1" id="discount_credits" 
/>
</li>

<li>
Apply Credits2: 
<input type="range" min="0" max="100" 
name="discount_credits2" id="discount_credits" 
/>
</li>

<span id="slidernumber">50</span>

Try this:

$(document).ready(function(){
  $("[type=range]").change(function(){
    var newval=$(this).val();
    $("#slidernumber").text(newval);
  });
});

http://jsfiddle.net/MahshidZeinaly/CgQ5c/

Now lets assume that you have a few sliders, but you want to see a text change if a particular one of them were moved. (I assume it because the range input is inside li tag, and you gave it a name):

<li>
Apply Credits1: 
<input type="range" min="0" max="100" 
name="discount_credits1" id="discount_credits" 
/>
<span id="slidernumber">50</span>
</li>

<li>
Apply Credits2: 
<input type="range" min="0" max="100" 
name="discount_credits2" id="discount_credits" 
/>
<span id="slidernumber">50</span>
</li>

Try this:

$(document).ready(function(){
  $("[type=range]").change(function(){
    var newv=$(this).val();
    $(this).next().text(newv);
  });
});

http://jsfiddle.net/MahshidZeinaly/2pe7P/1/

Comments

4

The on change event seems to be working correctly for me: http://jsfiddle.net/zV3xD/7/

<input type="range"  min="0" max="100" name="discount_credits" id="discount_credits" />

<span id="newValue" value="0">0</span>

$('#discount_credits').change( function() {
    var newValue = this.value;        
    $('#newValue').html(newValue);
});​

Or you can try something like this:

<form oninput="result.value=parseInt(a.value)">
    <input type="range" name="a" value="50" /> =
    <output name="result">0</output>
</form>

Using the output example here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/output

2 Comments

It only updates the #newValue when I release the slider button, on keyup. In Google Chrome Version 34.0.1847.116 using jQuery 2.0.3.
Hi @CarolineSchnapp - I've updated my example to show another way of approaching this issue. If it isn't sufficient, please let me know and we can figure out something that will.
-1

Several test and bug fixed!

$('input[name=form_range]').change('mousestop',function () {

});

1 Comment

This doesn't solve OPs problem (of capturing the new value while the user is still holding the mouse button pressed down): jsfiddle.net/zV3xD/7

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.