Search code examples
javascriptjqueryjquery-uijquery-ui-slider

jQuery UI Slider not changing default Value


I have setup a slider with jQuery UI, and the correct default value of 60 is displayed. but the slider is still on the very left. It should move to value 60.

https://i.sstatic.net/r2Xfg.jpg

Tried different options found here on StackOverflow but nothing helped.

var gewichtSlider = $("#gewicht-slider").slider({
    min: 40,
    max: 200,
    slide: function(event, ui) {
        $("#gewicht").val(ui.value).trigger('change');
        $("#gewicht-label").text(ui.value);
        $("#gewicht-handle").text(ui.value + ' kg');
    }
});
gewichtSlider.slider('option', 'slide').call(gewichtSlider, null, {value: 60});

The slider is not moving.

JSfiddle: https://jsfiddle.net/tz6pea5c/


Solution

  • The default value of slider is set using value property. To show initial value in your label, you need to add a create callback, which is being called right after the slider was initialized. Then, you need to add a slide callback to react to sliding event and change label's value accordingly. Lastly, I removed a hidden input field and showed you how to retrieve a value from the slider itself using a button click. If you need a value to be saved in a hidden input field instead, then you can modify setSliderValue function like that:

    function setSliderValue(value) {
      $sliderLabel.text(value + " kg");
      $("#your-hidden-field-id").val(value);
    }
    

    Bellow is the full working example:

    var defaultValue = 60;
    var $sliderLabel = $(".slider-after");
    
    function setSliderValue(value) {
      $sliderLabel.text(value + " kg");
    }
    
    var $gewichtSlider = $("#gewicht-slider").slider({
        min: 40,
        max: 200,
        value: defaultValue,
        create: function() {
          setSliderValue(defaultValue);
        },
        slide: function(event, ui) {
        	setSliderValue(ui.value);
        }
    });
    
    $("#current-value").click(function() {
    	alert($gewichtSlider.slider( "value" ) + " kg");
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
    
    
    <div class="gewicht-container question-container">
      <label for="gewicht">Wie schwer bist du?</label>
      <p class="question-desc">Dies ist eine wichtige Angabe für die Matraztenhärte. Daten werden nicht gespeichert ;)</p>
    
      <div class="slider">
        <div class="slider-before">
          Gewicht
        </div>
        <div id="gewicht-slider">
          <div id="gewicht-handle" class="ui-slider-handle"></div>
        </div>
        <div class="slider-after">
        </div>
        <div class="clearfix"></div>
      </div>
    
      <hr/>
      <button id="current-value">Get current value</button>
    </div>