2

I have the following slider in my razor

<input id="rbSlider" type="text" class="span2" value="" data-slider-min="0" data-slider-max="1000" data-slider-step="5" data-slider-value="[0,1000]" />

On Click, i need to get its current Min and Max values.

i tried using following without luck,

    var min = $('#rbSlider').data('slider').min;
    var max = $('#rbSlider').data('slider').max;

Its returning not defined error, How do i get min and max?

Burak
  • 5,252
  • 3
  • 22
  • 30
Nithin Paul
  • 2,169
  • 3
  • 33
  • 55

5 Answers5

3

You almost had it. The attributes data-slider-min="0" data-slider-max="1000" can be accessed like this:

var min = $('#rbSlider').data('sliderMin');
var max = $('#rbSlider').data('sliderMax');

Note that I have removed the - and used CamelCase. Take a look at the API and you will see that jQuery takes $( "div" ).data( "lastValue" ) === 43;:

... searches among the attributes of the element, converting a camel-cased string into a dashed string and then prepending data- to the result. So, the string lastValue is converted to data-last-value.

So for consistency, use camel casing (just watch out for this)

Community
  • 1
  • 1
jcuenod
  • 55,835
  • 14
  • 65
  • 102
  • Thanks, that was it :) – Nithin Paul Jun 24 '15 at 06:38
  • @jc uenod, hey i am back again, the reason its not working now. Slider not returning the correct Min and Max values. It always returns data-slider-min="0" data-slider-max="1000" values. It was working fine before. – Nithin Paul Jul 01 '15 at 09:25
  • @NithinPaul If it was working before, then it's not a result of this code - check your console. – jcuenod Jul 01 '15 at 20:01
  • @NithinPaul are you trying to get the current value? – jcuenod Jul 01 '15 at 20:03
  • Yes, I need currently selected Min and Max value. Even if there is no slider action occured, i need whatever the value its holding as Min and Max – Nithin Paul Jul 02 '15 at 03:57
2

You were so close. Try this.

var min = $('#rbSlider').data('slider-min');
var max = $('#rbSlider').data('slider-max');
Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60
  • Thanks, that was it :) – Nithin Paul Jun 24 '15 at 06:38
  • hey i am back again, the reason its not working now. Slider not returning the correct Min and Max values. It always returns data-slider-min="0" data-slider-max="1000" values. It was working fine before – Nithin Paul Jul 01 '15 at 09:26
  • This solution returns current value, u need to check if current values are changed, accordingly it will give results. – Nitin Varpe Jul 01 '15 at 11:19
  • I checked this, but it is only returning 0 and 1000 from data-slider-min="0" data-slider-max="1000" not its current selected value. What i am doing is when slide change occurs, i calls a script function to get the value from this $('#rbSlider'). But value none other than min value 0 and max value 1000 – Nithin Paul Jul 01 '15 at 11:49
  • min and maxvalue are always static i think, you need current value? – Nitin Varpe Jul 01 '15 at 12:11
  • Yes exactly. I need currently selected Min and Max value. Even if there is no slider action occured, i need whatever the value its holding as Min and Max – Nithin Paul Jul 01 '15 at 12:24
  • Here value is retrieved from slider. Also can I know, How to set value for the slider and reflect in UI. – Ninad Pingale Dec 06 '16 at 11:00
1

Oh finally i got it working using the following

var min = $('#rbSlider').data('slider').options.value[0];
var max = $('#rbSlider').data('slider').options.value[1];
Nithin Paul
  • 2,169
  • 3
  • 33
  • 55
1

Improvement for dynamically set limits

@jcuenod's answer will retrieve the min/max values as set in the initial HTML's DOM. However, if you are changing the limits later via JavaScript (i.e. $("#rbSlider").slider('option', {min: 5, max: 100})), this will not get you the up-to-date values

Instead, you need to use $("#rbSlider").data('slider').options.max

Addison Klinke
  • 1,024
  • 2
  • 14
  • 23
0
var min = $('#rbSlider').slider('option','min');
var max = $('#rbSlider').slider('option','max');
Ravi Shankar
  • 153
  • 1
  • 5
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 01 '17 at 14:47