ekbayrak2 ekbayrak2 - 11 days ago 6
Javascript Question

Bootstrap sliders and values

I have this piece of code. I have 3 bootstrap sliders with different values. I would like to have the input box show the value of slider1+slider2+slider3, at this moment it only shows the value of the first slider in the inputbox. Any help is appreciated. http://jsfiddle.net/4c2m3cup/

<div class="wrapper">
<input id="ex1" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" />
<hr />

<input id="ex2" data-slider-id='ex1Slider2' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="10" />
<hr />
<input id="ex3" data-slider-id='ex1Slider3' type="text" data-slider-min="0" data-slider-max="4" data-slider-step="1" />

<hr />

<input type="text" class="form-control" id="inputValue" value="0" />


</div>


and

var minSliderValue = $("#ex1").data("slider-min");
var maxSliderValue = $("#ex1").data("slider-max");

$('#ex1').slider({
value : 0,
formatter: function(value) {
return 'First Number: ' + value;
}
});

$('#ex2').slider({
value : 0,
formatter: function(value) {
return 'Second number: ' + value;
}
});

$('#ex3').slider({
value : 0,
formatter: function(value) {
return 'Third Number : ' + value;
}
});

// If You want to change input text using slider handler
$('#ex1').on('slide', function(slider){
$("#inputValue").val(slider.value);
});

// If you want to change slider using input text
$("#inputValue").on("keyup", function() {
var val = Math.abs(parseInt(this.value, 10) || minSliderValue);
this.value = val > maxSliderValue ? maxSliderValue : val;
$('#ex1','ex2','ex3').slider('setValue', val);
});

Answer

You need to calculate value on each slider event slide and get value from all three sliders. Here is jsfiddle

<div class="wrapper">
<input class="slider" id="ex1" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" />
    <hr />

<input class="slider" id="ex2" data-slider-id='ex1Slider2' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="10" />
     <hr />
    <input  class="slider" id="ex3" data-slider-id='ex1Slider3' type="text" data-slider-min="0" data-slider-max="4" data-slider-step="1" />

     <hr />

    <input type="text" class="form-control" id="inputValue" value="0" />


</div>

And javascript

var minSliderValue = $("#ex1").data("slider-min");
var maxSliderValue = $("#ex1").data("slider-max");

$('#ex1').slider({
    value : 0,
    formatter: function(value) {
        return 'RAM: ' + value + 'GB';
    }
});

$('#ex2').slider({
    value : 0,
    formatter: function(value) {
        return 'Disk Space: ' + value + 'GB';
    }
});

$('#ex3').slider({
    value : 0,
    formatter: function(value) {
        return 'CPU : ' + value + ' Cores';
    }
});

// If You want to change input text using slider handler
$('.slider').on('slide', function(slider){

  var val1 = $("#ex1").val();
  var val2 = $("#ex2").val();
  var val3 = $("#ex3").val();
  $("#inputValue").val(parseInt(val1)+ parseInt(val2)+ parseInt(val3));

});

// If you want to change slider using input text
$("#inputValue").on("keyup", function() {
    var val = Math.abs(parseInt(this.value, 10) || minSliderValue);
    this.value = val > maxSliderValue ? maxSliderValue : val;
    $('#ex1','ex2','ex3').slider('setValue', val);
});
Comments