84

I'd like to modify the sliders on-the-fly. I tried to do so by using

$("#slider").slider("option", "values", [50,80]);

This call will set the values, but the element will not update the slider positions. Calling

$("#slider").trigger('change');

does not help either.

Is there another/better way to modify the value AND slider position?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jAndy
  • 231,737
  • 57
  • 305
  • 359

13 Answers13

154

It depends on if you want to change the sliders in the current range or change the range of the sliders.

If it is the values in the current range you want to change, then the .slider() method is the one you should use, but the syntax is a bit different than you used:

$("#slider").slider('value',50);

or if you have a slider with two handles it would be:

$("#slider").slider('values',0,50); // sets first handle (index 0) to 50
$("#slider").slider('values',1,80); // sets second handle (index 1) to 80

If it is the range you want to change it would be:

$("#slider").slider('option',{min: 0, max: 500});
alexteg
  • 2,830
  • 4
  • 24
  • 21
  • 7
    that works so far, but it's not executing the event handler. The event handler updates some span's on sliding. It seems like I can't trigger the slide event manually ? – jAndy May 14 '10 at 11:18
  • 1
    ive tried both .slider('value', 100) and .slider('option', 'value', 100) and the slider does not move. am i mistaken in thinking it SHOULD move? or does this just change the VALUE, and i have to refresh or something to actually SEE the slider move? didnt want to ask a repeat question when it seems like this answer is so close to what i actually need. – trh178 Mar 11 '11 at 15:56
  • 2
    If you have a range slider you need to use the index integer (start 0) of the handle which you want to move. So to move the first handle (index 0) to 50 use $("#slider").slider('values',0,50); If you want to move the second slider (index 1) to 50 use $("#slider").slider('values',1,50); – alexteg Mar 22 '11 at 15:10
  • 4
    @jAndy setting the slider values will call the `change` event handler, not the `slide` event handler. – Igor Mukhin Jun 06 '13 at 13:35
  • 6
    you can also set a range slider with an array like so: $("#slider").slider('values', [50,80]); – tObi Jul 06 '14 at 18:01
  • What worked for me was to set `$("#slider").slider('value', 50, 80);` – Alonso Urbano Jun 28 '17 at 16:46
16

For me this perfectly triggers slide event on UI Slider :

hs=$('#height_slider').slider();
hs.slider('option', 'value',h);
hs.slider('option','slide')
       .call(hs,null,{ handle: $('.ui-slider-handle', hs), value: h });

Don't forget to set value by hs.slider('option', 'value',h); before the trigger. Else slider handler will not be in sync with value.

One thing to note here is that h is index/position (not value) in case you are using html select.

Imran Zahoor
  • 2,521
  • 1
  • 28
  • 38
sakhunzai
  • 13,900
  • 23
  • 98
  • 159
  • This solution worked perfectly for me using jQuery UI 1.8.24. – Александр Фишер Jan 28 '13 at 12:33
  • 1
    For me too... only this solution worked.. jQuery 1.8.3 and jQuery UI 1.9.2 I mean from @alexteg's answer, values were being set properly but the slider was not sliding. – Gaurav Pandey Aug 05 '13 at 08:19
  • A modified script for the range slider would be like: `$('#minPrice, #maxPrice').blur(function() { var max = parseInt($('#maxPrice').val()); var min = parseInt($('#minPrice').val()); if (min > max) return false; var hs = $("#slider-range"); hs.slider("values", 0, min); hs.slider("values", 1, max); hs.slider('option', 'slide').call(hs, null, {handle: $('.ui-slider-handle', hs), values: [min, max]}); });` Where #minPrice and #maxPrice are two text boxes. – Gaurav Pandey Aug 05 '13 at 08:21
9

None of the above answers worked for me, perhaps they were based on an older version of the framework?

All I needed to do was set the value of the underlying control, then call the refresh method, as below:

$("#slider").val(50);
$("#slider").slider("refresh");
Mal
  • 123
  • 1
  • 2
5

Mal's answer was the only one that worked for me (maybe jqueryUI has changed), here is a variant for dealing with a range:

$( "#slider-range" ).slider('values',0,lowerValue);
$( "#slider-range" ).slider('values',1,upperValue);
$( "#slider-range" ).slider("refresh");
bentwonk
  • 193
  • 2
  • 10
3

If you need change slider values from several input, use it:

html:

<input type="text" id="amount">
<input type="text" id="amount2">
<div id="slider-range"></div>

js:

    $( "#slider-range" ).slider({
    range: true,
    min: 0,
    max: 217,
    values: [ 0, 217 ],
    slide: function( event, ui ) {
        $( "#amount" ).val( ui.values[ 0 ] );
        $( "#amount2" ).val( ui.values[ 1 ] );
    }
    });

$("#amount").change(function() {
    $("#slider-range").slider('values',0,$(this).val());
});
$("#amount2").change(function() {
    $("#slider-range").slider('values',1,$(this).val());
});

https://jsfiddle.net/brhuman/uvx6pdc1/1/

Human
  • 141
  • 1
  • 4
3

One part of @gaurav solution worked for jQuery 2.1.3 and JQuery UI 1.10.2 with multiple sliders. My project is using four range sliders to filter data with this filter.js plugin. Other solutions were resetting the slider handles back to their starting end points just fine, but apparently they were not firing an event that filter.js understood. So here's how I looped through the sliders:

$("yourSliderSelection").each (function () {
    var hs = $(this); 
    var options = $(this).slider('option');

    //reset the ui
    $(this).slider( 'values', [ options.min, options.max ] ); 

    //refresh/trigger event so that filter.js can reset handling the data
    hs.slider('option', 'slide').call(
        hs, 
        null, 
        {
            handle: $('.ui-slider-handle', hs),
            values: [options.min, options.max]
        }
    );

});

The hs.slider() code resets the data, but not the UI in my scenario. Hope this helps others.

globalSchmidt
  • 1,329
  • 16
  • 28
2

It's possible to manually trigger events like this:

Apply the slider behavior to the element

var s = $('#slider').slider();

...

Set the slider value

s.slider('value',10);

Trigger the slide event, passing a ui object

s.trigger('slide',{ ui: $('.ui-slider-handle', s), value: 10 });
anonymous
  • 29
  • 1
1

I wanted to update slider as well as the inputbox. For me following is working

a.slider('value',1520);
$("#labelAID").val(1520);

where a is object as following.

 var a= $( "<div id='slider' style='width:200px;margin-left:20px;'></div>" ).insertAfter( "#labelAID" ).slider({
            min: 0,
            max: 2000,
            range: "min",
            value: 111,
            slide: function( event, ui ) {

                $("#labelAID").val(ui.value    );
            }
        });

        $( "#labelAID" ).keyup(function() {
            a.slider( "value",$( "#labelAID" ).val()  );
        });
Ashish Agarwal
  • 6,215
  • 12
  • 58
  • 91
1

In my case with jquery slider with 2 handles only following way worked.

$('#Slider').slider('option',{values: [0.15, 0.6]});
Jonzi
  • 141
  • 1
  • 2
  • 13
0

This worked for me but it's a custom implementation.

Set slider values

  $('#slider-width').slider('values', 0, range[ 0 ]);
  $('#slider-width').slider('values', 1, range[ 1 ]);
  setSliderHandles('width', range[ 0 ], range[ 1 ]);

Move handles in position

  function setSliderHandles(type, min, max){
    $('#slider-' + type + ' span.ui-slider-handle:eq(0) .price-range-min').html(min + '"');
    $('#slider-' + type + ' span.ui-slider-handle:eq(1) .price-range-max').html(max + '"');
    $('#slider-' + type + ' span.price-range-both').html('<i>' + min + '" - </i>' + max + '"');
    
    if ( min == max ) {
      $('#slider-' + type + ' span.price-range-both i').css('display', 'none');
      $('#single_' + type).val(min);
    } else {
      $('#slider-' + type + ' span.price-range-both i').css('display', 'inline');
      $('#single_' + type).val('');
    }

    if (collision($('#slider-' + type + ' span.price-range-min'), $('#slider-' + type + ' span.price-range-max')) == true) {
        $('#slider-' + type + ' span.price-range-min, #slider-' + type + ' span.price-range-max').css('opacity', '0');  
        $('#slider-' + type + ' span.price-range-both').css('display', 'block');        
    } else {
        $('#slider-' + type + ' span.price-range-min, #slider-' + type + ' span.price-range-max').css('opacity', '1');  
        $('#slider-' + type + ' span.price-range-both').css('display', 'none');     
    }        
  }

function collision($div1, $div2) {
    var x1 = $div1.offset().left;
    var w1 = 40;
    var r1 = x1 + w1;
    var x2 = $div2.offset().left;
    var w2 = 40;
    var r2 = x2 + w2;
      
    if (r1 < x2 || x1 > r2) return false;
    return true;
}
Adrian P.
  • 5,060
  • 2
  • 46
  • 47
-1

On start or refresh value = 0 (default) How to get value from http request

 <script>
       $(function() {
         $( "#slider-vertical" ).slider({
    animate: 5000,
    orientation: "vertical",
    range: "max",
    min: 0,
    max: 100,
    value: function( event, ui ) {
        $( "#amount" ).val( ui.value );

  //  build a URL using the value from the slider
  var geturl = "http://192.168.0.101/position";

  //  make an AJAX call to the Arduino
  $.get(geturl, function(data) {

  });
  },
    slide: function( event, ui ) {
        $( "#amount" ).val( ui.value );

  //  build a URL using the value from the slider
  var resturl = "http://192.168.0.101/set?points=" + ui.value;

  //  make an AJAX call to the Arduino
  $.get(resturl, function(data) {
  });
  }
  });

$( "#amount" ).val( $( "#slider-vertical" ).slider( "value" ) );

});
   </script>
IgorFX
  • 1
-1

Finally below works for me

$("#priceSlider").slider('option',{min: 5, max: 20,value:[6,19]}); $("#priceSlider").slider("refresh");

Vijay Chauhan
  • 367
  • 3
  • 7
-3

Here is working version:

var newVal = 10;
var slider = $('#slider');        
var s = $(slider);
$(slider).val(newVal);
$(slider).slider('refresh');
nvvetal
  • 1,756
  • 1
  • 17
  • 19