Skip to content Skip to sidebar Skip to footer

Jquery Slider Background Color

I am new to web programming and I am trying to create website that contains slider. I am currently using jquery to create the slider. I am trying to change the background color of

Solution 1:

jQuery UI uses the property background which is more important than your property background-color. Change your $( "#slider-range-max" ).css("background-color","#ff0000"); to $( "#slider-range-max" ).css("background","#ff0000"); and it works.

Also, don't forget to put the background change into your else statement, or else it'll stay red, even when amount is more than 5.

Solution 2:

Look at this: jsFiddle. See if this works for what you need.

$(function() {
    $("#slider-range-max" ).slider({
      range:"min",
      min:0,
      max:10,
      value:0,
      step: .001,
      slide: function( event, ui ) {
        $("#amount" ).val( ui.value );
        if(ui.value < 5){
          $("#amount").attr("style","border:0; color:#ff0000; font-weight:bold;");
          $('#slider-range-max').removeClass('green');
          $('#slider-range-max').addClass('red');

        }
         else{
          $("#amount").attr("style","border:0; color:#00ff00; font-weight:bold;");
            $('#slider-range-max').removeClass('red');
          $('#slider-range-max').addClass('green');
         }

      }
    });
    $("#amount" ).val( $("#slider-range-max" ).slider( "value" ) );
  });


.ui-widget-header{
    background: none;
}
.red .ui-slider-range {
    background-color:#ff0000;
}
.green .ui-slider-range {
    background-color:#00ff00;
}

Solution 3:

Use an if statement like this. The default color should be red (#ff0000) and when the value of #amount is > 5 it animates the color to green.

if($('#amount').val(); > 5){
    $('#amount').animate({"color":"#00ff00"}, 1000);
}else{
    $('#amount').animate({"color":"#ff0000"}, 1000);

Alternately you could use $('#amount').css("color":"#00ff00"); to change it instantly to green instead of an animation.

Quick tip: use single quotes for your jQuery selectors. Double quotes will break some selectors like this $("#elem[type="text"]") because it ends the open quote with the first closed quote it finds.

Post a Comment for "Jquery Slider Background Color"