Search code examples
jqueryjquery-uiswitch-statementsliders

All my (jQuery UI) single sliders get the same value


I want each slider of the same page have it's own value when I slide each one of them.

When I slide one of them, the "slider block" of each slider works as planned as the conditional background-color, but the value is the same for all the sliders.

Here's my JS code:

$(document).ready(function() {
  $('.slider').slider({
    value: 5,
    min: 1,
    max: 10,
    step: 1,
    slide: function( event, ui ) {
      $( ".ui-slider-handle" ).text(ui.value);
      switch(ui.value){
        case 1: { $(this).css('background', 'darkred'); break;}
        case 2: { $(this).css('background', 'red'); break;}
        case 3: { $(this).css('background', 'orange'); break;}
        case 4: { $(this).css('background', 'gold'); break;}
        case 5: { $(this).css('background', 'yellow'); break;}
        case 6: { $(this).css('background', '#AAFF00'); break;}
        case 7: { $(this).css('background', 'lime'); break;}
        case 8: { $(this).css('background', 'cyan'); break;}
        case 9: { $(this).css('background', '#00AAFF'); break;}
        case 10: { $(this).css('background', 'blue'); break;}
      }
    }
  });
  $(".ui-slider-handle").val($('.slider').slider( "value" ));
});

And here's the code in JSfiddle: http://jsfiddle.net/Arkl1te/Wrq3H/

Is there a way to fix that? I appreciate your help!


Solution

  • It's not that they have the same value, the problem is that you're updating both handles each time that a slide occurs. Possible fix: http://jsfiddle.net/Wrq3H/3/

    //change text for current's slider handle only
            $(this).children(".ui-slider-handle").text(ui.value);