11

I have a slider with values 1 - 5. it updates a hidden input with the ID of 'days'.

$(function() {
    $("#slider").slider({
        value: 3,
        min: 1,
        max: 5,
        step: 1,
        slide: function(event, ui) {
            $("#days").val(ui.value);
        }
    });
    $("#days").val($("#slider").slider("value"));
});​

I want to add labels to the slider

so at position 1 it would say 1 hour, 2 would say 12 hours, 3 would say 1 day, 4 = 3 days and 5 = 1 week.

but i want to keep the value of days as 1 -5

how is this possible?

EDIT

Want to copy this slider example

j08691
  • 204,283
  • 31
  • 260
  • 272
Vince Lowe
  • 3,521
  • 7
  • 36
  • 63

3 Answers3

17

Just arrange your label container (e.g. I added a div) and when you slide, update it.

Live version : http://jsfiddle.net/8ek4a/5/

Code :

$(function() {
   var labelArr = new Array("", "1 hour", "12 hours", "1 day", "3 day", "1 week");
   $( "#slider" ).slider({
       value:3,
       min: 1,
       max: 5,
       step: 1,
       slide: function( event, ui ) {
          $( "#days" ).val( ui.value );
          $("#label").html(labelArr[ui.value]);
       }
   });
   $( "#days" ).val($( "#slider" ).slider( "value" ) );
   $("#label").html(labelArr[$( "#slider" ).slider( "value" )]);
});​

UPDATE: Now I have come up with this solution to implement indicator. see http://jsfiddle.net/8ek4a/6/

tusar
  • 3,364
  • 6
  • 37
  • 60
  • @arttronics ! oh thanks for correcting it. I missed the day<>hour relationship :( – tusar Jun 26 '12 at 13:05
  • Excellent use of Array which I did not know how to implement in UI Slider. Thanks! – arttronics Jun 26 '12 at 13:22
  • Hi, Thanks but not quite what im after. see updated question with screenshot. i want to show the labels so you know what you are selecting. – Vince Lowe Jun 26 '12 at 13:58
  • 1
    hi @VinceLowe ! I have updated the solution with indicator, please see the updated link. You could customize the UI according to your need. – tusar Jun 26 '12 at 15:13
0

you can try this code

 $(function() {
$("#slider-range").slider({
  range: true,
  min: 12,
  max: 500,
  values: [ 75, 300 ],
  slide: function( event, ui ) {
    $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
  }
});
$( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) + "- $" + $( "#slider-range" ).slider( "values", 1 ) );
});
Marc
  • 6,154
  • 1
  • 15
  • 10
0

Now we can use this plugin to display values

$(".slider").slider().slider("pips", {
        first: "pip",
        last: "pip"
    }).slider("float");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.10.4/themes/flick/jquery-ui.css" rel="stylesheet"/>
<link href="https://raw.githubusercontent.com/simeydotme/jQuery-ui-Slider-Pips/master/dist/jquery-ui-slider-pips.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<script src="https://raw.githubusercontent.com/simeydotme/jQuery-ui-Slider-Pips/master/dist/jquery-ui-slider-pips.js"></script>

<div class="slider"></div>

Fiddle

Dan
  • 329
  • 4
  • 16