23

How can I have a jQuery slider that is readonly? One that would display a value but the user wouldn't be allowed to move it?

Thanks

William Perron
  • 485
  • 7
  • 16

7 Answers7

23

The documentation says there's a .slider('disable') function - not sure what that actually does to the slider element itself, but that may be an option for you.

Documentation here

brettkelly
  • 27,655
  • 8
  • 56
  • 72
  • 2
    This is making the slider disappear (not being rendered). –  Jun 09 '09 at 14:26
  • Then I suppose the easiest way to do it would be to position a transparent element on top of the slider (thus preventing the user from moving it manually) and control its position using the .value attribute. – brettkelly Jun 09 '09 at 14:31
  • I'm using jquery-ui-1.9.2 and `.slider("disable")` works like a charm. It decreases the transparency and disables dragability. – nthpixel Sep 17 '13 at 05:51
  • 1
    Any way to keep the transparency as it is but disable dragability at the same time? – Thomas Sebastian Oct 09 '14 at 09:58
  • @ThomasSebastian try to add `$(".ui-slider-disabled.ui-state-disabled").removeClass("ui-state-disabled");` after you call it. – Mowd Feb 26 '19 at 03:19
7

I tried all suggested. Only this works:

$('#mySlider').slider({ disabled: true });
JJ_Coder4Hire
  • 4,706
  • 1
  • 37
  • 25
6

Disabling it makes it transparent. If you want it to look like a normal slider, you can override slide handler to return false:

    $("#mySlider").on("slide", function (event, ui) { return false; });
3

I have got the same issue that the slider disappears if I use directly $('#mySlider').slider( 'disable' );. I have loaded the slider fisrt..then disabled it. Though it is not a good way, but it works for me.

$('#mySlider').slider(
            {
                range: "min",
                min: 0,
                max: 100,                
                value: $("span", this).text(), 

                }

            });
$('#mySlider').slider( 'disable');
miti737
  • 471
  • 2
  • 7
  • 18
1

You would simply have to do this:

$('#mySlider').slider( 'disable' );
karim79
  • 339,989
  • 67
  • 413
  • 406
0

$("#slider").slider("disable") works fine, however reenabling doesn't work $("#slider").slider("enable")

Harry
  • 87,580
  • 25
  • 202
  • 214
Mark Vital
  • 960
  • 3
  • 14
  • 25
-1

I had a client request for this today. Its probably a good idea to do this as a best practice and prevent string input.

Since the slider sets the value in an <input> tag, you can make it "readonly".

<p>
  <label for="amount">Price range:</label>
  <input type="text" id="amount" style="border: 0; color: #f6931f; font-weight: bold;" readonly />
</p>

<div id="slider-range"></div>

Fiddle

Ken
  • 207
  • 1
  • 11
  • 1
    This is not what the OP asked for. He wanted the slider to be disabled, which is not what your example shows. – pierus Feb 03 '15 at 19:24