javascriptjquerybootstrap-slider

How to console each slider value only when they are moved?


I have 3 slider ranges:

<div class="container">
  <div class="row" id="time" >
    <div class="col-xs-4">
      Days: <b>1</b> <input id="days" type="text" class="span2" value="" data-slider-min="1" data-slider-max="31" data-slider-step="1" data-slider-value="[0,31]"/> <b>31</b>
    </div>
    <div class="col-xs-4">
      Months: <b>January</b> <input id="months" type="text" data-provide="slider" data-slider-step="1" data-slider-min="0" data-slider-max="12" data-slider-tooltip="show" data-slider-value="[0,12]" /> <b>December</b>
    </div>
    <div class="col-xs-4">
      Years: <b>0</b> <input id="years" type="text" class="span2" value="" data-slider-min="0" data-slider-max="2017" data-slider-step="1" data-slider-value="[0,2017]"/> <b>Today</b>
    </div>
  </div>
</div>

And I am running the following js:

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
getData = function() {
    var data = {};
  var temp = $('#days').slider().val().split(',');
  data.days = temp[0];
  temp = $('#months').slider().val().split(',');
  data.months = months[temp[0]];
  temp = $('#years').slider().val().split(',');
  data.years = temp[0];
  console.log(data);
}
$("#days, #years").slider({
  tooltip: 'always',
});  
$('#months').slider({
    formatter: function(value) {
    return months[value[0]] + " : " + months[value[1] - 1];
    }
});
$("input").on("slide", function(slideEvt) {
    getData();
});

Basically when we move a slider, the console outputs the current value of the current value and output is:

{days: "12", months: "July", years: "1396"}

That is fine but if I move the second slider because I want to do a range, the second value isn't showing while it should be:

{days: "12", days: "24", months: "July", months: "September", years: "1396", years: "1842"}

But each value or both, should only appear when we actually move each slider

Here it is a jsFiddle, check console

Note, I am actually going to need the values with this format as they will be considered as classes for some elements:

Example

day22 
february
year1632

Solution

  • Objects cannot have duplicate keys, so you'll have to use a different data structure. You could for instance let data be an array of two objects, and then each of those two objects would have a day, month and year property:

    getData = function() {
        var data = [];
        for (var i = 0; i < 2; i++) {
            data.push({
                day: $('#days').slider().val().split(',')[i], 
                month: months[$('#months').slider().val().split(',')[i]],
                year: $('#years').slider().val().split(',')[i], 
            }) ;
        }  
        console.log(data);
    }
    

    Or, if you prefer to have the days together in a pair, and the same for the months and years, you could let each of those three properties be an array with two values:

    getData = function() {
        var data = {
            days: $('#days').slider().val().split(','), 
            months: $('#months').slider().val().split(',').map(i => months[i]),
            years: $('#years').slider().val().split(',')
        }  
        console.log(data);
    }
    

    Another issue

    The months slider is allowing one value too many (there are 13 values between 0 and 12 inclusive), so you need to make this change to the month slider properties:

    data-slider-max="11"
    

    and in the code:

    $('#months').slider({
        formatter: function(value) {
            return months[value[0]] + " : " + months[value[1]]; // not -1
        }
    });